:)
Thank you in advance for helping me with this issue:
I've an entity Hotel which has a ManyToMany relation with an entity HotelService
how can i build (with QueryBuilder if possible) one query to select all hotels having a subset of services given as array parameter?
Example: H1(S1, S2, S3, S4), H2(S2, S3, S4), H3(S1, S2, S3)
Querying with subset (S1, S2) has to return H1 and H3.
I have tried many things, this is some code extract:
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();
}
This code return all hotels for which there is ONE service id in $arrayIds.
I want the opposite (hotels for which services contain ALL ids in $arrayIds).
Of course, inversing parameters in expr()->in doesn't solve the problem, and create bad parameters errors.
Can somebody help me please? (sorry for my bad english) :)