php - 学说查询 WHERE IN - 多对多

标签 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);
}

这是酒店实体的属性(property)声明
/**
 * @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;

我目前得到的错误是:

[Semantical Error] line 0, col 177 near 'boardBasis I': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.



提交表单和使用 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]
              ...

我似乎找不到这个查询的正确语法,我过去做过几次(每次都很痛苦),但我不记得它是如何完成的。我尝试不映射 ID,而是通过了 ArrayCollection直接进去。

目前我唯一能想到的就是将其切换为使用 createQuery并使用 DQL,看看这是否有什么不同。

对这个问题的任何帮助将不胜感激,谢谢

最佳答案

在我看来,您的加入还没有完全完成。您缺少描述要加入的字段的语句:

$qb = $repo->createQueryBuilder("a")
    ->innerJoin("AppBundle:BoardType", "b")
    ->where("a.boardBasis = b.id")
    ...

或者你可以这样加入:
$qb = $repo->createQueryBuilder("a")
    ->innerJoin("a.boardBasis", "b")
    ...

然后你可以添加你的 WHERE IN像这样的声明:
$qb->andWhere('b.id IN (:ids)')
    ->setParameter('ids', $ids);

关于php - 学说查询 WHERE IN - 多对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37091363/

相关文章:

php - 将 Unix 时间戳转换为小时

php - php中的继承速度

json - Symfony2 - 实体 findAll 在 API 上返回大量响应

symfony - 如何使用命令在 Symfony 3.4 中生成实体?

doctrine-orm - 数据库模式与当前映射文件不同步(但它是!)

mysql - 导入数据库时​​,学说实体的外键约束失败

Mysql错误1451但找不到引用id的行

php - Windows 7 上的 Zend PHP 上传文件权限

用于大文件解析的 Php 脚本在中间中断

json - 如何使用FOSRestBundle和Symfony 2自定义异常格式?