以下是我的模特:
class Subject extends AppModel {
public $belongsTo = array('SubjectGroup');
}
class SubjectGroup extends AppModel {
public $hasMany = array('Subjects');
}
我正在检索控制器中的所有SubjectGroup,如下所示:
$this->SubjectGroup->find('all', array('order' => 'SubjectGroup.name'));
但是我如何告诉它按Subject.name命令每个SubjectGroup中的主题?
答案 0 :(得分:5)
解决方案:
class SubjectGroup extends AppModel {
public $hasMany = array(
'Subject' => array('order' => 'Subject.name')
);
}
感谢chetan patel提醒我高级选项。
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany
答案 1 :(得分:3)
$this->SubjectGroup->find('all', array('order' => array('SubjectGroup.name'=>'asc','Subject.name'=>'asc')));
或
class SubjectGroup extends AppModel {
public $hasMany = array(
'Subject'=>array(
'className' => 'Subject',
'order' => 'Subject.name DESC', // order by descending time of Subjects
'foreignKey' => 'subject_id')
);
}