2012-04-17 90 views
0

所以,下面這段代碼:CakePHP查找所有生成的SQL,返回重複記錄(具有相同的ID)。查找列表不

$friends = $this->find('all', 
     array(
      'conditions' => array(
       'User.id' => 102 
      ), 
      'contain' => 'Profile' 
     ) 
    ); 

生成此SQL:

SELECT `User`.`id`, `User`.`sForceId`, `User`.`householdId`, 
`User`.`householdSForceId`, `User`.`username`, `User`.`primaryUser`, `User`.`password`, 
`User`.`firstName`, `User`.`lastName`, `User`.`dateOfBirth`, `User`.`email`, 
`User`.`preferredEmail`, `User`.`phone`, `User`.`preferredPhone`, `User`.`homePhone`, 
`User`.`workPhone`, `User`.`mobilePhone`, `User`.`ethnicity`, `User`.`ethnicityOther`, 
`User`.`religion`, `User`.`religionOther`, `User`.`active`, `User`.`adminStatus`, 
`User`.`group`, `User`.`created`, `User`.`modified`, `Profile`.`id`, 
`Profile`.`userId`, `Profile`.`aboutMe`, `Profile`.`picture`, `Profile`.`created`, 
`Profile`.`modified`, `Profile`.`lat`, `Profile`.`lng` FROM `users` AS `User` LEFT JOIN 
`profiles` AS `Profile` ON (`Profile`.`userId` = `User`.`id`) WHERE `User`.`id` = (102) 

(道歉,如果閱讀,讓你的大腦受到傷害)

這SQL代碼選擇相同記錄三次。我不知道爲什麼。它出什麼問題了?更重要的是,如何更改我的CakePHP代碼來選擇記錄一次而不是三次?

如果有幫助:用戶屬於家庭和hasOne配置文件。

+0

你有附加到用戶的多個簡介:

您可以通過查看手動數據庫檢查呢?我會手動檢查數據庫'SELECT * FROM profiles where profiles.userId = 102'。如果你有一個以上的,那麼連接將返回3行,因此創建三個記錄在數組中。 – jeremyharris 2012-04-17 15:54:32

+0

太棒了!將其添加爲答案,我會接受它。謝謝。 – Will 2012-04-17 15:56:12

+0

酷,很高興它幫助你看着辦吧! – jeremyharris 2012-04-17 16:04:32

回答

3

如果你不知何故最終有不止一個資料,是用戶,你將與多個記錄,由於LEFT JOIN返回每個配置文件的紀錄告終。

這適用於hasOne和屬於關聯關係,因爲他們是使用LEFT JOIN s到加入了數據的人。 SELECT * FROM profiles where profiles.userId = 102

相關問題