crowhill crowhill - 6 months ago 22
PHP Question

Yii: default sort model by related table

I have a three tables, "pets", "species" and "homes." Homes have multiple pets, each of which has a species. The species can be mixed in a home.

Is it possible to use

findByPk()
on a house and have the associated pets sorted by species then name?

Hendersons
--------------
Cat "Hairball"
Cat "Princess"
Dog "Ace"
Dog "Killer"


This is probably blindingly obvious to someone, but I'm not finding an answer.

Answer

With Carlos' help, here's the functional code, in "homes" model:

'pets' => array(self::MANY_MANY, 'Pets', 'home_id',
                'order'=>'species.name ASC',
                'with'=>'species')

Pretty simple. Thanks, Carlos.

Comments