Sunday, January 30, 2011

Sorting by a (second level) associated model using paginate helper

Dear all,

I suspect this is either really easy or very hard. I've been through
endless apparently similar questions on here but haven't been able to
figure out the answer.

I have a "Quote" model, which I am trying to list in a view. Quote
belongsTo Attendance which belongs to Person. I am trying to sort the
list of quotes by the first_name field in the Person model.

The array being sent to the view is

Array
(
[0] => Array
(
[Quote] => Array
(
[id] => 1
[attendance_id] => 15
[quote_text] => Hello

)

[Attendance] => Array
(
[id] => 15
[person_id] => 2
[trip_id] => 7
[Person] => Array
(
[id] => 2
[first_name] => John
[last_name] => Smith

)

)
)


}
This seems to work fine, and in the view, I can echo out

foreach ($quotes as $quote) {
echo $quote['Attendance']['Person']['first_name'];
}
without any problem.

What I cannot get to work is accessing/using the same variable as a
sort field in paginate

echo $this->Paginator->sort('Name',
'Attendance.Person.first_name');
or

echo $this->Paginator->sort('Location',
'Attendance.Trip.location');

Does not work. It appears to sort by something, but I'm not sure what.
Am I using the correct syntax?. Is there something I should be doing
in a model or controller to make this work?

Thanks in advance.

Peter

--
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
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: