CakePHP分页计数不匹配查询?

时间:2011-08-19 10:26:32

标签: php mysql cakephp pagination cakephp-1.3

我有一个相当修改的分页查询使用了许多联接等 - 但由于某种原因,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);
    }

示例连接(所有连接表和数据表都有内部连接):

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

样本条件:

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

3 个答案:

答案 0 :(得分:10)

这是因为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

答案 1 :(得分:1)

想出来:

paginator计数器依赖$this->find('count')返回结果总和的整数,由于某种原因,它不喜欢'group'参数。因此,遵循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']));添加到控制器中,以便我可以访问分页参数。

答案 2 :(得分:0)

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);
           }

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

相关问题