简体   繁体   English

CakePHP:根据另一个表中的值过滤对一个表的搜索

[英]CakePHP: Filter search on one table based on values from another table

I have a table containing user information, model name is User 我有一个包含用户信息的表,型号名称为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. 我猜想这将需要一个IN子句和一个联接,但是我不知道在我的代码中设置它的“蛋糕”方法。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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