简体   繁体   中英

CakePHP: Filter search on one table based on values from another table

I have a table containing user information, model name is User

User
----
id    full_name (for demo purposes)
1     john doe
2     jane doe
3     john smith
4     jane smith

The User model is linked to another model called Record

Record
------
userid(FK)
1
1
3
3
3

I also have a routine in my Controller that looks up users based on some criteria.

$leads = $this->User->find('list',array(
'fields' => array('User.id','User.full_name'),
'order' => array('User.full_name ASC'),
'conditions'=>array('AND'=>array(
'NOT'=>array('User.deleted_record'=>1),
 array('NOT'=>array('User.username'=>'root',
  array('User.username'=>'testuser')))
        ))
));

This works just fine for returning a dropdown list of users. What I need to do now is further filter the user list based on whether they have one or more records in the Record model. If they don't, they should not appear.

User
----
id
1
3

I'm guessing this will require an IN clause and a join, but I don't know the "Cake" way to set it up in my code.

如果已经在各个模型中的用户和记录表之间建立了关系,则应在UsersController中尝试以下操作:

$userList = $this->User->Record->find('list', array('group' => array('Record.userid')));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM