php - Symfony2 - Doctrine2 QueryBuilder WHERE IN ManyToMany 字段

标签 php mysql symfony doctrine-orm doctrine

:)

预先感谢您帮助我解决此问题:

我有一个实体 Hotel,它与实体 HotelService 具有多对多关系

我如何构建(如果可能的话使用 QueryBuilder)一个查询来选择所有具有以数组参数形式给出的服务子集的酒店?

示例:H1(S1, S2, S3, S4), H2(S2, S3, S4), H3(S1, S2, S3)

使用子集(S1,S2)查询必须返回H1和H3。

我尝试了很多东西,这是一些代码摘录:

public function findByServices($services) {
 
    $qb = $this->createQueryBuilder('hotel')
 
            ->addSelect('location')
 
            ->addSelect('country')
 
            ->addSelect('billing')
 
            ->addSelect('services')
 
            ->innerJoin('hotel.billing', 'billing')
 
            ->innerJoin('hotel.location', 'location')
 
            ->innerJoin('location.city', 'city')
 
            ->innerJoin('location.country', 'country');
 
            ->innerJoin('hotel.services', 'services');
 
    $i = 0;
    $arrayIds = array();
    foreach ($services as $service) {
        $arrayIds[$i++] = $service->getId();
    }
    $qb->add('where', $qb->expr()->in('services', $arrayIds))->getQuery();
}

此代码返回 $arrayIds 中有一个服务 ID 的所有酒店。

我想要相反的(服务包含 $arrayIds 中所有 id 的酒店)。

当然,反转 expr()->in 中的参数并不能解决问题,还会产生错误的参数错误。

有人可以帮我吗? (抱歉我的英语不好):)

最佳答案

对于您的解决方案,您可以将 COUNT(DISTINCT)HAVINGGROUP BY 子句结合使用

public function findByServices($services)
{
    $qb = $this->createQueryBuilder('hotel')
        ->addSelect('location')
        ->addSelect('country')
        ->addSelect('billing')
        ->addSelect('services')
        ->addSelect('COUNT(DISTINCT  services.id) AS total_services')
        ->innerJoin('hotel.billing', 'billing')
        ->innerJoin('hotel.location', 'location')
        ->innerJoin('location.city', 'city')
        ->innerJoin('location.country', 'country')
        ->innerJoin('hotel.services', 'services');
    $i = 0;
    $arrayIds = array();
    foreach ($services as $service) {
        $arrayIds[$i++] = $service->getId();
    }
    $qb->add('where', $qb->expr()->in('services', $arrayIds))
        ->addGroupBy('hotel.id')
        ->having('total_services = '.count($arrayIds))
        ->getQuery();
}

在上面的查询中,我又添加了一个选择来计算每个酒店的不同服务 ID,即

->addSelect('COUNT(DISTINCT services.id) AS HIDDEN total_services')

然后我还需要一个用于该计数的分组依据,所以我添加了

->addGroupBy('hotel.id')


现在出现了棘手的部分,正如您提到的,您需要拥有所有服务 ID(如 id (1,2,3))的酒店,因此当我们在其执行或操作中使用时,应返回包含这 3 个服务的酒店,如其中servid_id = 1或servid_id = 2 servid_id = 3这正是您不希望酒店必须有这3个的AND操作,所以我通过部分转换了这个逻辑

->having('total_services = '.count($arrayIds))

现在 total_services 是查询的虚拟别名,并保存每个酒店的不同计数,因此我将此计数与 IN() 中提供的 id 计数进行比较这将返回必须包含这些服务的酒店


GROUP BY and HAVING Clause

关于php - Symfony2 - Doctrine2 QueryBuilder WHERE IN ManyToMany 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25576818/

相关文章:

MySql 查询列属性

php - 如何从 MySQL 中的列中选择总值?

symfony - 手动身份验证检查 Symfony 2

php - 部分删除/销毁 $_SESSION 数据? PHP

PHP 初学者 : How to "Order by" that query?

php - 无法在 cpanel 中使用 php 显示 Longblob 图像

php - 这个 php 脚本中会发生太多连接吗? (mysql_select_db)

php - Wordpress 发布上一个/下一个箭头替换 I.E. rel ="next">«</a> - 到处搜索,找不到代码?

php - 在 getResult 上强制瀑布水化

bash - symfony 服务器 :start command throwing an error