When using the following query only 24 records are returned as two clients have more than one pet who meet the criteria but Doctrine will not return the additional records in my Zend App.
$q = Doctrine_Query::create()
->select('c.clientID,c.firstname,c.lastname,c.address1,c.address2,c.address3,t.county,p.name')
->from('PetManager_Model_Clients c')
->leftJoin('c.PetManager_Model_Pets p')
->leftJoin('c.PetManager_Model_Counties t')
->leftJoin('c.PetManager_Model_Groomappointments g')
->where('p.type=2 AND g.gapmtClient IS NULL');
The following MySQL query returns 26 records, can anyone tell me how to replicate it in Doctrine
mysql> Select DISTINCT c.clientid,c.firstname,c.lastname,p.name
-> from (clients AS c left join pets as p on c.clientid =p.owner) left join groomappointments AS g on g.gapmtclient=c.clientid
-> where p.type=2 AND g.gapmtclient is null;
This is because you’re selecting from Clients and not Pets. Doctrine will return all 24 distinct clients, with their pet(s) as related records. The two clients with more than one matching pet will each have two related pet entities.
If you want a record for each pet (rather than one for each distinct client), you should select from Pets first, and then join to the other tables.
Something like: