使用以下查询时,只返回24条记录,因为两个客户有多个宠物符合条件,但Doctrine不会在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');
以下MySQL查询返回26条记录,任何人都可以告诉我如何在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;
答案 0 :(得分:0)
这是因为您选择的是客户而不是宠物。 Doctrine将返回所有24个不同的客户,其宠物作为相关记录。拥有多个匹配宠物的两个客户将分别拥有两个相关的宠物实体。
如果您想要每只宠物的记录(而不是每个不同客户的记录),您应该先从Pets中选择,然后加入其他表格。
类似的东西:
$q = Doctrine_Query::create()
->select('c.clientID,c.firstname,c.lastname,c.address1,c.address2,c.address3,t.county,p.name')
->from('PetManager_Model_Pets p')
->leftJoin('p.PetManager_Model_Clients c')
->leftJoin('c.PetManager_Model_Counties t')
->leftJoin('c.PetManager_Model_Groomappointments g')
->where('p.type=2 AND g.gapmtClient IS NULL');