【发布时间】:2014-10-23 23:16:20
【问题描述】:
:)
提前感谢您帮助我解决这个问题:
我有一个实体 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 中取反参数并不能解决问题,并且会产生错误参数错误。
有人可以帮帮我吗? (对不起我的英语不好):)
【问题讨论】:
标签: php mysql symfony doctrine-orm doctrine