【问题标题】:Paginate based on the contained table on cakephp根据 cakephp 上包含的表格进行分页
【发布时间】:2013-04-06 07:05:49
【问题描述】:

我正在使用 cakephp 开发 CMS,我有 2 个表需要通过一些参数进行过滤,并且为了优化我必须从“hasMany”模型开始进行查询,简单的方法是在“belongsTo”上进行" 模型。

class Client extends AppModel {
    public $actsAs = array('Containable');

    public $hasMany = array('Projects');
}

class Project extends AppModel {
    public $actsAs = array('Containable');

    public $belongsTo = array('Client');
}

我在 $conditions 数组中有 where 条件。

$this->paginate = array(
                'Client' => array(
                    'limit' => 20,
                    'conditions' => $conditions,
                    'contain' => array(
                        'Project' => array(
                            //'limit' => 20
                        )
                    )
                )
            );
$this->set('clients', $this->paginate('Client'));

通过这种方式,我获得了 20 个客户和每个客户内的所有项目。如果我取消注释该行,我会在 20 个客户中的每个客户中获得 20 个项目。

我想要总共 20 个项目,不管有多少客户(最多 20 个)。

谁能帮帮我?

提前致谢。

【问题讨论】:

    标签: php cakephp cakephp-2.0 pagination


    【解决方案1】:

    如果你想展示projects,你应该给project分页;

    $this->paginate = array(
        'Project' => array(
            'limit' => 20,
                'conditions' => $conditions,
    
                // no need to set a limit on 'client',
                // as each project belongs to only one client
                'contain' => array(
                    'Client'
                )
            )
    );
    
    $this->set('projects', $this->paginate('Project'));
    

    可能需要将Project 模型添加到控制器内的$uses 数组中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-24
      • 2018-10-21
      • 2016-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-21
      相关资源
      最近更新 更多