douhan6738 2011-10-09 21:02
浏览 25
已采纳

CakePHP - 列出按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

-Classroom 1

-Classroom 2

-Classroom 3

Program 2

-Classroom 1

-Classroom 2... etc

My index action in 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

  • 写回答

1条回答 默认 最新

  • dpnru86024 2011-10-10 05:43
    关注

    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. Note that you don't need the $this->Classroom->recursive statement if you use Containable.

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突