Find condition in cakePHP with Containable behavior
I have 2 models, User and Evolution. User has many Evolution, Evolution belongs to User.
I have 10 0开发者_JAVA技巧00 users and I want to do a condition that give me 500 users that DON'T have today's date in the Evolution table.
Originally, I was taking every one in the USER table and then I was looking if they had their row in Evolution like:
$user=$this->User->find('all',array('contain' => array(
'Evolution' => array('conditions' => array('date'=>$lastupdate))
),
'fields' => array(
'name',
'id',
)
));
and then looking if there was a row or not :
if(empty($user['Evolution']))
{
But I think that is farely stupid to take 10 000 users and foreach them all to just take 500.
How can I do my find to directly have 500 users that DO NOT have today's date in Evolution table.
Thanks!!
What about doing something like this:
$evolutions = $this->User->Evolution->find('all',
array(
'conditions'=>array('date !='=>$lastupdate),
'fields'=>array('MAX(date) as max_date', 'user_id', 'User.name', 'User.email'),
'group'=>array('user_id', 'User.name', 'User.email')
),
'contain'=>array('User'),
'limit'=>500
)
This way, you search in evolutions table, but because they are linked you will have the user's record for each evolution.
The problem will come if there are more than one evolution per day, but then you can play with MAX() somehow.
How about
$user=$this->User->find('all',array('contain' => array(
'Evolution' => array('conditions' => array('date !='=>$lastupdate))
),
'fields' => array(
'name',
'id',
)
'limit'=>500
));
精彩评论