【问题标题】:CakePHP Pagination count not matching query?CakePHP 分页计数不匹配查询?
【发布时间】:2023-03-21 10:56:02
【问题描述】:

我有一个相当修改的分页查询,它使用了一些连接等 - 但由于某种原因,paginator->counter() 从不匹配计数查询的结果。

您可以在 http://dev.qreer.com/ 上看到它的运行情况 - 通过选择 LHS 导航上的各种选项,查询输出如下,并且分页器计数似乎非常随机。

知道我可以从哪里开始调试吗?

在作业控制器中:

    $this->paginate = $this->Job->paginateParams($data);
    $jobs = $this->paginate('Job');
    $this->set(compact('jobs'));

在模型中:

function paginateParams($data = null){
        //lots of joins + conditions
        return array('contain' => $contain, 'recursive' => 0,'joins' => $joins, 'conditions' => $conditions, 'group' => 'Job.id', 'order' => $order);
    }

Sample Join(所有连接表和数据表都有内连接):

        array(
            'table' => 'education_backgrounds',
            'alias' => 'EducationBackground',
            'type' => 'INNER',
            'conditions' => array('EducationBackgroundsJobs.education_background_id = EducationBackground.id'),
        ),

样品条件:

      'EducationBackground.name' => array('Aerospace Engineering');

【问题讨论】:

  • 我昨天遇到了同样的问题,当您在分页查询中有一个组时会发生这种情况。没有找到解决办法...

标签: php mysql cakephp pagination cakephp-1.3


【解决方案1】:

因为group by 我找到了解决方法。我很想放链接,但我把它弄丢了,所以我会发布代码:

public function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
    $parameters = compact('conditions', 'recursive');
    if (isset($extra['group'])) {
        $parameters['fields'] = $extra['group'];
        if (is_string($parameters['fields'])) {
            // pagination with single GROUP BY field
            if (substr($parameters['fields'], 0, 9) != 'DISTINCT ') {
                $parameters['fields'] = 'DISTINCT ' . $parameters['fields'];
            }
            unset($extra['group']);
            $count = $this->find('count', array_merge($parameters, $extra));
        } else {
            // resort to inefficient method for multiple GROUP BY fields
            $count = $this->find('count', array_merge($parameters, $extra));
            $count = $this->getAffectedRows();
        }
    } else {
        // regular pagination
        $count = $this->find('count', array_merge($parameters, $extra));
    }
    return $count;
}

我在 app_model 中添加了它,它对我来说很好:)

希望对你有帮助

已编辑:我找到了链接 =)

http://wiltonsoftware.com/posts/view/custom-group-by-pagination-and-a-calculated-field

【讨论】:

  • 谢谢!这几乎正​​是我所制定的 - 但这绝对是“包罗万象”的解决方案。
【解决方案2】:

想通了:

分页计数器依靠$this->find('count') 返回结果总数的整数,由于某种原因,它不喜欢“组”参数。所以按照Custom Query Pagination(也建议在页面底部为任何自定义/修改的分页自己进行计数) - 我在我的模型中添加了以下内容:

function paginateCount(){
    $params = Configure::read('paginate.params');
    $params['fields'] = 'DISTINCT (Job.id)';
    unset($params['group']);
    unset($params['contain']);
    unset($params['order']);
    return $this->find('count', $params);
}

这会用正确的值覆盖该值,并且一切似乎都运行良好。

请记住,我已将 Configure::write('paginate', array('params' => $this->paginate['Job'])); 添加到我的控制器中,以便我可以访问分页参数。

【讨论】:

    【解决方案3】:

    function paginateCount($conditions = null, $recursive = 0,$extra) {

                       $db = $this->getDataSource();
                               $sql = $db->buildStatement(
                                       array(
                                       'fields'     => array('DISTINCT Gdi.id'),
                                       'table'      => $db->fullTableName($this),
                                       'alias'      => 'Gdi',
                                       'limit'      => null,
                                       'offset'     => null,
                                       'joins'      => isset($extra['joins'])?$extra['joins']:null,
                                       'conditions' => $conditions,
                                       'order'      => null,
                                       'group'      =>isset($extra['group'])?$extra['group']:null
                                       ),
                               $this
                               );
    
    
    
                       $this->recursive = $recursive;
                       $results = $this->query($sql);
    
                       return count($results);
               }
    

    只需在您的模型中添加此函数并更改字段名称和模型名称即可。使用此功能,您可以自定义自己的查询计数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-26
      • 2020-03-03
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      • 2010-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多