I can see what you want to achieve, but in your specific example it
doesn't make sense.
Each baker has MANY cookies associated with him, so you can't sort by
a list; only by a 1:1 field.
So you don't want to sort the bakers by the rate of their baked
cookies - you need to be much more specific.
How about sorting the bakers by the rate of their personal best? Or by
their personal worst? Or some other, more complicated metric (mean
average for example)
As you can see, you need to create a 1-to-1 relationship between Baker
and Cookie, dynamically bind on it, and perform the sort on that
cookie's rating.
If you have Baker hasMany Cookie, you'd do it like this (I know you've
got a HABTM, but this is easier to explain - HABTM is similar but more
long-winded)
$this->Bakery->Baker->bindModel(array('hasOne' => array('BestCookie'
=> array('className' => 'cookie', 'order' => 'BestCookie.rating
DESC'))));
$this->Bakery->hasMany['Baker']['order'] = 'BestCookie.rating DESC';
$bakeries = $this->Bakery->findAll(...);
(The order clause in the hasOne binding specifies the best cookie -
use ASC for the worst. The second order clause is what order the
bakers appear in - here I've done best to worst - use ASC for worst to
best)
On Oct 26, 2:22 pm, David Spreekmeester <[EMAIL PROTECTED]>
wrote:
> Thanks for replying, Paul. So I seem not to be the only one, do none
> of the gurus here have a clue how to do this in a tidy efficient way
> without sorting afterwards?
>
> On Oct 26, 3:04 am, "Paul Webster" <[EMAIL PROTECTED]> wrote:
>
> > I tried to do something very similar not so long ago, and too had trouble
> > getting it to do as I wanted.
>
> > In the end not having time to pursue it further I did just sort after the
> > query was made, as far as I could tell due to the way habtm's are querried
> > it wasn't a simple case of altering the association settings etc.
>
> > I'd be interested to see if anyone has some clear thoughts on this too.
>
> > -----Original Message-----
> > From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf
>
> > Of David Spreekmeester
> > Sent: Friday, 26 October 2007 9:02 a.m.
> > To: Cake PHP
> > Subject: ordering a model through related model order
>
> > Hey there bakers.
>
> > I couldn't find an appropriate answer to this anywhere. Suppose I have
> > three models:
>
> > Bakery <- hasAndBelongsToMany -> Baker <- hasAndBelongsToMany ->
> > Cookie
>
> > Suppose there is a cookie baking contest going on. Every cookie is
> > rated for quality (int Cookie.quality). I would like to do a find()
> > for a certain bakery, and then *sort the employed bakers there by the
> > rate of their baked cookies*. I tried stuff like setting the 'order'
> > property to 'Cookie.quality' in the Cookie model file, but of course
> > that just orders the cookies, and doesn't sort the bakers. The same
> > goes for setting 'Cookie.quality' as the sorting parameter in the
> > find() call.
>
> > Of course I could just sort the bakers afterwards with my own custom
> > sort function, but is there a way to do this automagically?
>
> > __________ NOD32 2617 (20071025) Information __________
>
> > This message was checked by NOD32 antivirus system.http://www.eset.com
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" 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
-~----------~----~----~----~------~----~------~--~---