Here are my models:
class Subject extends AppModel {
public $belongsTo = array('SubjectGroup');
}
class SubjectGroup extends AppModel {
public $hasMany = array('Subjects');
}
I am retrieving all SubjectGroups in my controller like this:
$this->Subj开发者_如何学运维ectGroup->find('all', array('order' => 'SubjectGroup.name'));
But how do I also tell it to order the Subjects within each SubjectGroup by Subject.name?
Solution:
class SubjectGroup extends AppModel {
public $hasMany = array(
'Subject' => array('order' => 'Subject.name')
);
}
Thanks to chetan patel for reminding me of the advanced options.
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany
$this->SubjectGroup->find('all', array('order' => array('SubjectGroup.name'=>'asc','Subject.name'=>'asc')));
or
class SubjectGroup extends AppModel {
public $hasMany = array(
'Subject'=>array(
'className' => 'Subject',
'order' => 'Subject.name DESC', // order by descending time of Subjects
'foreignKey' => 'subject_id')
);
}
精彩评论