CakePHP分页计数不匹配查询? [英] CakePHP Pagination count not matching query?

查看:177
本文介绍了CakePHP分页计数不匹配查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



您可以在 http://dev.qreer.com/ 上查看相关信息,方法是选择LHS导航上的各种选项,查询输出如下,并且分页器计数似乎是随机的。



任何想法,我可以开始调试这个? p>

在工作控制器中:

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

在模型中:

  function paginateParams($ data = null){
//很多连接+条件
return array('contains'=> $ contains,'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'=>数组('航天工程'); 


解决方案

这是因为 by 我发现了一个解决方法。我想放置链接,但我失去了,所以我会post代码:

  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'])){
//使用单个GROUP BY字段进行分页
if(substr($ parameters ['fields'],0,9) !='DISTINCT'){
$ parameters ['fields'] ='DISTINCT'。 $ parameters ['fields'];
}
unset($ extra ['group']);
$ count = $ this-> find('count',array_merge($ parameters,$ extra));
} else {
//对多个GROUP BY字段使用低效的方法
$ count = $ this-> find('count',array_merge($ parameters,$ extra)
$ count = $ this-> getAffectedRows();
}
} else {
//正式分页
$ 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


I've got a fairly modified pagination query using a number of Joins etc - but for some reason the paginator->counter() never matches the results from the count query.

You can see it in action at http://dev.qreer.com/ - by choosing various options on the LHS navigation, the query output is below and the paginator count appears to be pretty random.

Any idea where I can start looking to debug this?

In the Jobs Controller:

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

In the Model:

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 (there's inner joins for all the join tables and data tables):

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

Sample Condition:

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

解决方案

It's because of the group by I found a workaround. I'd love to put the link but i've lost it, so i'll post the code:

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

I added it in the app_model and it works fine for me :)

Hope this helps

Edited: I found the link =)

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

这篇关于CakePHP分页计数不匹配查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆