>>> You'd probably be best off creating a view (in the database, not a
>>> Cake view) to do this. Off the top of my head, the query would use a
>>> self join to grab the most recent record for each client. With proper
>>> indexing, it should be fairly efficient.
>>
>> That seems like overkill to me, it's a simple and common query with a
>> little processing of the data. As long as the requests are paginated
>> it should never be an issue.
>>
>
> Overekill? Sorry, but I can't agree with that. It's just the opposite.
> Doing this sort of thing inside any decent database will always beat
> doing it in application code. Your suggestions all entail selecting
> everything and throwing out what's not needed. That can't be anything
> but less efficient. Just because Cake is very handy with a lot of
> things doesn't mean that good database practice should be ignored.

In terms of learning how to solve his problem from within cake, a view
is overkill. In terms of writing an efficient query I'd say it was
overkill. If all he needs is a single extra field (the date of the
most recent client), then that could be grabbed using a single line of
SQL, it doesn't require a view at all.

I'm certainly not suggesting getting everything and throwing out
what's not needed. Rather I'm suggesting get a few Client records at a
time, and with the correct values in the Contain you can have just the
details for the most recent report. Something like:

// Attach Containable if not already attached
$this->Client->Behaviors->attach('Containable');

// Set contain params for Client model
$this->Client->contain(array(
        'Report'=>array(
                'fields'=>'Report.id', 'Report.created',
                'limit'=>1,
                'order'=>'Report.created desc'
        )
));

// set some defaults for paginate
$this->paginate = array(
        'limit'         => 60,
        'page'          => 1
);
// Grab data via the paginate method
$this->data = $this->paginate('Client');
// set in view
$this->set('data', $this->data);

// Loop over in view
foreach($data as $row):
      echo 'name: '.$row['Client']['name'];
      if (isset($row['Report'][0])):
              echo 'last report created: '.$row['Report'][0]['created'];
      endif;
endforeach;

cheers,

Jon

-- 

jon bennett
w: http://www.jben.net/
iChat (AIM): jbendotnet Skype: jon-bennett

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to