CakePHP: How do I order results based on a 2-level deep association model?

Posted by KcYxA on Stack Overflow See other posts from Stack Overflow or by KcYxA
Published on 2010-04-02T20:46:19Z Indexed on 2010/04/03 11:43 UTC
Read the original article Hit count: 315

Filed under:

I'm hoping I won't need to resort to custom queries.

A related question would be: how do I retrieve data so that if an associated model is empty, no record is retrieved at all, as opposed to an empty array for an associated model?

As an oversimplified example, say I have the following models: City --> Street --> House

How do I sort City results by House numbers?

How do I retrieve City restuls that have at least one House in it? I don't want a record with a city name and details and an empty House array as it messes up pagination results.

CakePHP retrieves House records belonging to the Street in a separate query, so putting somthing like 'House.number DESC' into the 'order' field of the search query returns a 'field does not exist' error.

Any ideas?

© Stack Overflow or respective owner

Related posts about cakephp