开发者

CakePHP - findAll() ordering of hasMany model

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')
    );
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜