views:

47

answers:

2

Hi,

I have 2 models, User and Evolution. User has many Evolution, Evolution belongs to User.

I have 10 000 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!!

+1  A: 

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.

Nik
That does not work because I want to find the evolution that DON'T have the $lastupdate.
Jean-Nicolas
I've updated the answer. Basically the SpawnCxy give you the solution for the conditions :)
Nik
This can't work because I want to know which user do NOT have today's evolution row. By doing this condition I will get every user other's day so I dont have the information that I need.
Jean-Nicolas
As I told you you can play with MAX and GROUP BY - see the updated answer.
Nik
+1  A: 

How about

$user=$this->User->find('all',array('contain' => array(

            'Evolution' => array('conditions' => array('date !='=>$lastupdate))
        ),
    'fields' => array(
        'name',
        'id',
    )
    'limit'=>500
));
SpawnCxy
This way you will limit the Evolution, but the relation between User and Evolution will be unchanged and you could have in the result also users which has update within today. But the condition part is right.
Nik