2011-06-05 97 views
0

当使用以下查询时,仅返回24条记录,因为两个客户有多个符合条件的宠物,但Doctrine不会返回我的Zend App中的其他记录。原则1.2.3不选择所有记录

$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条记录,任何人都可以告诉我如何复制它在教义

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

这是因为你来自客户端,而不是宠物选择。学说将返回所有24个不同的客户,他们的宠物作为相关记录。具有多于一个匹配宠物的两个客户每个都有两个相关的宠物实体。

如果你想为每个宠物记录(而不是每个不同的客户),你应该先从宠物中选择,然后加入其他表格。

喜欢的东西:

$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'); 
+0

感谢清理这件事 – Graham 2011-06-05 18:11:14