views:

267

answers:

3

I'm using the Containable behavior to get a list of Comments (belongsTo Post, which belongs to Question; Question hasMany Post, and Post hasMany Comments; all of these belong to Users).

$data = $this->Question->find ( 'first', 
    array ('contain' => 
        array ('User', 
               'Post' => array ('User', /* 'order' => 'User.created DESC'*/ )
        ) 
    ) 
);

It works, when I comment out the section in comments above. I suppose this is to be expected, but what I want is all of the Posts that are found, should be sorted in order of the 'created' field of the 'User' they belong to. How do I accomplish this deeper level sorting in CakePHP? I always get, "Warning (512): SQL Error: 1054: Unknown column 'User.created' in 'order clause'"

Thanks for your help!

A: 

Check your recursive value. If it's too limiting, it will ignore the containable links, IIRC. I remember bumping into this a few times. I'd try containing multiple models, but my recursive option was set to 0 and nothing would get pulled. For your example, I'd think that a value of 1 (the default) would suffice, but maybe you've explicitly set it to 0 somewhere?

Rob Wilkerson
I actually leave the recursive option out, since I believe Containable will choose the most appropriate option for you (if you specify it, things can only get worse).
Jasie
That's not been my experience. It's been a while, but I vaguely recall losing hours at one point because I assumed the same thing. It could be that my memory is inaccurate on this point.
Rob Wilkerson
A: 

You can add before your call to find() the following:

 $this->Question->order = 'Question.created DESC';
pcp
This will sort the Question by its `created` field, which is not what I want. Perhaps you meant `$this->Question->Post->order = 'User.created DESC';` ?
Jasie
+1  A: 

Also, you might be trying to group on a related table from a find call that doesn't use joins.

Set your debug level to something greater than 1 so you can see the query log and make sure that Cake isn't doing two queries to fetch your data. If that is the case then the first query is not actually referencing the second table.

If you want to manually force a join in these situations you can use the Ad-Hoc joins method outlined by Nate at the following link.

http://bakery.cakephp.org/articles/view/quick-tip-doing-ad-hoc-joins-in-model-find

Abba Bryant
I looked at the debug output and you're right, it's not performing a `JOIN`. This is unfortunate, is there really no other way to get CakePHP to do it?
Jasie
http://teknoid.wordpress.com/2008/07/17/forcing-an-sql-join-in-cakephp/Has a workaround from teknoid on how to force a left join by unbinding the hasMany and replacing that with a temporary hasOne which does use joins. Read it and let me know if that works for you, otherwise I can post the ad-hoc joins code for you to look at.
Abba Bryant
Indeed, it did work. Thanks for the help!
Jasie
glad to help, sometimes finding what works in CakePHP is hard. There is a ton of old, out-dated info out there and the good stuff is sometimes buried.
Abba Bryant