1

I have this on a HABTM statement:

$this->set('usergroups', $this->User->find('first', array('conditions' => array('User.id' => $this->Auth->user('id')))));

This gets groups that are associated to the user id via a courses_users table.

This works perfectly find, except I also need to find all groups that a user doesn't belong to. How do I get the opposite of the statement above?

I used 'not' as a condition, and it still gave me the same result.

Thanks!

4

1 回答 1

1

像这样的东西应该工作:

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

旁注:你真的应该在你的 AppModel 中设置recursive-1,而不是依赖递归来返回额外的数据。相反,使用CakePHP 的 Containable Behavior

于 2013-11-13T19:04:33.593 回答