简体   繁体   中英

cakephp conditions based on HABTM relation

User Model has relation:

public $hasMany = array(
'MyRecipe' => array(
'className' => 'Recipe',
)
);

I want to select all users who have recipes with ID: 1,2

How I can use that conditions in select:

$this->User->find('all', array(
 'conditions' => array(
 'Recipe.Id' => [1,2]
)
));

But in this example I will get also Users without recipes, how to prevent that ?

please give this relation in User model

class User extends AppModel
{
    var $name = 'User';
    var $belongsTo = array("Recipe");
}

and in user controller your query as

$list = $this->User->find('all',array("conditions"=>array("recipe_id IN"=>  [1,2] )));

its gives output which you want..

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