2011-06-05 90 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