model conditions
I am trying to set conditions on relationships in a model to better filter records returned in finds. If I specify conditions on which users are returned in a $this->Post->find('list) call I'm still getting all the records returned instead of the conditions (booleanfield = 1) placed in the model.
User hasMany Post
Post belongsTo User
In the Post model:
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => array('User.booleanfield' => 1)
)
);
Obviously I could place that condition in the find call but isn't this what is supposed to happen when you put conditions in the model? I've also tried putting the condition in the User model under the hasMany variable but that doesn't work either. What am I doing wrong?
Asked by fly2279, on 10/5/10
1 Answer
I think You misunderstood this.
If You want to get users based on conditions this is the right way:
$this->User->find('all', 'conditions'=>array("User.booleanfield"=>1))
If You want to find users with their Post's (which are in example published), You can define this in relation:
class User{
$has_many => array(
'Post'=> array(
//...
'conditions' => 'Post.published = 1'
)
}
}
Is that the answer for Your question?
Answered by GrzegorzPawlikon 10/5/10
Your Answer
You can use Creole Wiki Syntax to format your text.
Tagged with
Rating
0
Viewed
199 times
Last Activity
on 10/5/10