grouping contained models

2019-08-09 04:17发布

PresentationView hasMany SlideView

SlideView model has field duration.

The presentation_duration = sum of all SlideView.duration fields from slide views that belong to the presentation.

I want to get list (paged) of presentations with presentation_duration for each presentation. So far I am trying like this:

$this->paginate['contain'] = array(
            'PresentationView' => array(
                'SlideView' => array(
                    'group' => 'SlideView.presentation_view_id',
                                    // 'conditions' => array('group' => 'SlideView.presentation_view_id'),
                )
            )
        ); 

The commented line is an option that I tried.

Both methods end up with some SQL errors:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'SlideView.group' in 'fieldlist'

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'group' in 'where clause'

It seems that contain doesn not even recognize group key - is it even possible to use grouping like this? If yes how I can do it?

1条回答
干净又极端
2楼-- · 2019-08-09 05:02

You cannot pass 'group' to your 'contain'. To do what you're hoping to do, you'll need to use JOINs - see CakePHP Joining Tables.

查看更多
登录 后发表回答