我在HABTM语句上有这个:
$this->set('usergroups', $this->User->find('first', array('conditions' => array('User.id' => $this->Auth->user('id')))));
这将获取通过courses_users表与用户 ID 关联的组。
这非常有效,除了我还需要找到用户不属于的所有组。我如何获得与上述陈述相反的结果?
我使用"not"作为条件,它仍然给了我相同的结果。
谢谢!
这样的事情应该可以工作:
$user = $this->User->find('first', array(
'conditions' => array(
'User.id' => $this->Auth->user('id')
)
));
$otherGroups = $this->Group->find('all', array(
'conditions' => array(
'NOT' => array('id' => Hash::extract($user, '{n}.Group.id'))
)
));
旁注:您实际上应该将recursive
设置为在 AppModel 中-1
,而不是依赖递归来返回其他数据。 相反,请使用 CakePHP 的可包含行为。