2014-11-23 48 views

回答

3

我相信hasOne关系会在SQL语句中创建一个LEFT JOIN。可以说,为您找到生成的SQL看起来是这样的(假设有多个列虽然):

SELECT User.id, User.name, Profile.id 
FROM users AS User 
LEFT JOIN profiles AS Profile 
    ON Profile.user_id = User.id; 

所有你需要做的就是添加一个WHERE子句:WHERE Profile.id IS NULL无与伦比行。因此改变你的CakePHP代码为

$this->User->find('all',array(
    'conditions' => array('Profile.id' => 'NULL'))) 
+1

事实上,我一直在做这与hasMany关系,返回一个空的数组,而不是一个单一的结果! 'NULL'也可以是php null。 – Mohsenme 2014-11-23 16:06:01