简体   繁体   English

CakePHP - 列出按program_id分组的条目

[英]CakePHP - listing entries grouped by program_id

I have the following relations between my models 我的模型之间有以下关系

Program hasMany Classroom
Classroom belongsTo Program

What I am trying to achieve is to display all the classrooms, grouped by program, for example: 我想要实现的是显示按程序分组的所有教室,例如:

Program 1 计划1

-Classroom 1 - 课堂1

-Classroom 2 - 课堂2

-Classroom 3 - 课堂3

Program 2 计划2

-Classroom 1 - 课堂1

-Classroom 2... etc - 课堂2 ......等

My index action in ClassroomsController: 我在ClassroomsController中的索引操作:

    function index() {

    $this->Access->grantAdmin();

    $this->Classroom->recursive = 1;
    $this->set('classrooms', $this->paginate());
}

I tried to use this: 我试着用这个:

    var $paginate = array(
    'group' => 'program_id'
);

But it didn't work as I expected. 但它并没有像我预期的那样奏效。 Any ideas? 有任何想法吗? Thanks 谢谢

Try adding: 尝试添加:

var $actsAs = array('Containable');

to the Program model. 到程序模型。 Then in the controller: 然后在控制器中:

$programs = $this->Classroom->Program->find('all', array(
    'contain' => array(
        'Classroom',
    ),
));
$this->set(compact('programs'));

This should return your data in the hierarchy you're looking for. 这应该返回您正在寻找的层次结构中的数据。 See the Containable behaviour documentation for more details. 有关更多详细信息,请参阅Containable行为文档。 Note that you don't need the $this->Classroom->recursive statement if you use Containable. 请注意,如果使用Containable,则不需要$this->Classroom->recursive语句。

Your own code example shows that you're using pagination. 您自己的代码示例显示您正在使用分页。 It should be fairly easy to put containable and pagination together by looking around the docs. 通过查看文档,将可包含和分页放在一起应该相当容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM