Scr*_*ble 4 php symfony doctrine-orm
我正在 Symfony2 中建立一个酒店网站。每家酒店都可以提供多种食宿选择,例如自助餐、全包等。
在我的搜索表单上,用户可以按所有常用字段进行过滤,例如位置、价格、星级和董事会基础。板基是一个多选复选框。
当用户选择多个板基选项时,我目前正在以这种方式处理它......(这是抛出错误)
$repo = $this->getDoctrine()->getRepository("AppBundle:Accommodation");
$data = $form->getData();
$qb = $repo->createQueryBuilder("a")
->innerJoin("AppBundle:BoardType", "b")
->where("a.destination = :destination")
->setParameter("destination", $data['destination'])
->andWhere("a.status = 'publish'");
if (count($data['boardBasis']) > 0) {
$ids = array_map(function($boardBasis) {
return $boardBasis->getId();
}, $data['boardBasis']->toArray());
$qb->andWhere($qb->expr()->in("a.boardBasis", ":ids"))
->setParameter("ids", $ids);
}
Run Code Online (Sandbox Code Playgroud)
这是酒店实体的财产声明
/**
* @ORM\ManyToMany(targetEntity="BoardType")
* @ORM\JoinTable(name="accommodation_board_type",
* joinColumns={@ORM\JoinColumn(name="accommodation_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="board_type_id", referencedColumnName="id")}
* )
*/
private $boardBasis;
Run Code Online (Sandbox Code Playgroud)
我目前得到的错误是:
[语义错误] 'boardBasis I' 附近的第 0 行第 177 列:错误:无效的 PathExpression。预期的 StateFieldPathExpression 或 SingleValuedAssociationField。
在提交表格并var_dump
在板上使用我得到的类型时:
object(Doctrine\Common\Collections\ArrayCollection)[3043]
private 'elements' =>
array (size=2)
0 =>
object(AppBundle\Entity\BoardType)[1860]
protected 'shortCode' => string 'AI' (length=2)
protected 'id' => int 1
protected 'name' => string 'All-Inclusive' (length=13)
protected 'description' => null
protected 'slug' => string 'all-inclusive' (length=13)
protected 'created' =>
object(DateTime)[1858]
...
protected 'updated' =>
object(DateTime)[1863]
...
1 =>
object(AppBundle\Entity\BoardType)[1869]
protected 'shortCode' => string 'BB' (length=2)
protected 'id' => int 2
protected 'name' => string 'Bed & Breakfast' (length=15)
protected 'description' => null
protected 'slug' => string 'bed-breakfast' (length=13)
protected 'created' =>
object(DateTime)[1867]
...
protected 'updated' =>
object(DateTime)[1868]
...
Run Code Online (Sandbox Code Playgroud)
我似乎找不到这个查询的正确语法,我过去做过几次(每次都很痛苦),但我不记得它是如何完成的。我试过不映射 ID,ArrayCollection
直接传入。
目前我唯一能想到的就是将其切换为使用createQuery
和使用 DQL,看看这是否有什么不同。
对这个问题的任何帮助将不胜感激,谢谢
在我看来,您的加入还没有完全完成。您缺少描述要加入的字段的语句:
$qb = $repo->createQueryBuilder("a")
->innerJoin("AppBundle:BoardType", "b")
->where("a.boardBasis = b.id")
...
Run Code Online (Sandbox Code Playgroud)
或者你可以这样加入:
$qb = $repo->createQueryBuilder("a")
->innerJoin("a.boardBasis", "b")
...
Run Code Online (Sandbox Code Playgroud)
然后你可以WHERE IN
像这样添加你的语句:
$qb->andWhere('b.id IN (:ids)')
->setParameter('ids', $ids);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6493 次 |
最近记录: |