Let's suppose I have two documents
Users and Purchases. All the purchase have a reference to a User, and Users have a field gender in it.
I want to select the products bought by male clients. So I have the gender and I want to iterate thought the purchases and define their "gender".
Something like that
public function findByGender()
{
$query =
$this->createQueryBuilder()
->field(""Purchase->User->gender"")->equals('male')
->getQuery()
;
}
But of course the code between ""... "" does not work. I would like to know if it is possible, and if is, how could I do it?
You need to separate this query in 2 queries. First select all male users and pass these users to second query. I recommend you dehydrate the queries and select only the essential.
$users = $this->createQueryBuilder()
->hydrate(false)
->select('_id')
->field('gender')->equals('male')
->getQuery()->execute()->toArray();
$products = $this->createQueryBuilder()
->hydrate(false)
->field('user.id')->in(array_keys($users))
->getQuery()->execute()->toArray();