I realize this question has been asked before in this forum, but none of the solutions I’ve found have worked.
Using Yii 1.0.10
I’ve got two tables: Teacher and Contact. Contact contains the contact information (including first and last name) of the teachers. What I want to do is query Teachers, joining on Contact, sorting by Contact.LastName.
In the Teacher model, I set up this relation:
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'contact'=>array(self::HAS_ONE, 'Contact', 'ID'),
);
}
I’ve tried various possibilities here, including “??” instead of contact, setting up the order by clause in the relation declaration in the model, setting up an “alias” for contact and using that, setting the order in the “with” clause - none of them work.
what is the right way to sort a recordset by a column in a related table in a one-to-one relationship?
I’m mystified. Seems like such a simple common thing to want to do.
I think (and someone please confirm this) that $criteria->order is used to sort the rows of the related table, and can’t be used to sort the rows of the primary table. However, CSort seems to do the trick…
I was able to solve this by doing the following:
add an alias to my relation in the Teacher model:
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'contact'=>array(self::HAS_ONE, 'MailListStudent', 'StudentID', 'alias'=>'contact')
);
}
in the Teacher Controller…
a. don’t set $criteria->order - it will return an “unknown column” error (I have no idea why)
b. instead set the "defaultOrder" of the CSort object
c. make sure you use the "with" syntax in your query to include the relation…