On Sun, Jan 23, 2011 at 4:56 AM, Toby G <[email protected]> wrote:
> Morning all,
>
> I'm a little stumped this morning, on how to sort a model's records
> based on the max value of it's associated (has many) dates.  Initially
> I used a find & then a sort, however I now need to paginate the
> results, so I need to be able to order the results as part of the
> paginated find.

If ModelA hasMany ModelB, and ModelB has some date column, how could
you possibly sort ModelA by those dates? That could only work if it
was a hasOne association.

ModelA (2 records)
id => 1
id => 2

ModelB (4 records):
id => 1
model_a_id => 1
date => '2010-01-01'

id => 2
model_a_id => 1
date => '2010-09-01'

id => 3
model_a_id => 2
date => '2010-06-01'

id => 4
model_a_id => 2
date => '2010-02-01'

Trying to order ModelA here by ModelB's date column couldn't work
because it would have to be done on an arbitrary ModelB record for
each ModelA record.

Or do you mean that you want to order ModelB by date within each
individual ModelA record?

ModelA => array(
        0 => array(
                id => 1
                ModelB => array(
                        0 => array(
                                id => 1,
                                model_a_id => 1,
                                date => '2010-01-01'
                        ),
                        1 => array(
                                id => 2,
                                model_a_id => 1,
                                date => '2010-09-01'
                        )
                )
        ),
        1 => array(
                id => 2
                ModelB => array(
                        0 => array(
                                id => 3,
                                model_a_id => 2,
                                date => '2010-02-01'
                        ),
                        1 => array(
                                id => 4,
                                model_a_id => 2,
                                date => '2010-06-01'
                        )
                )
        )
);

Or maybe I haven't had enough coffee yet.

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to