2011-08-19 147 views
4

我有一个相当修改的分页查询使用一些联接等 - 但出于某种原因paginator->计数器()从不匹配计数查询的结果。CakePHP分页计数不匹配查询?

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

任何想法,我可以开始寻找调试呢?

在作业控制器:

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

在Model:

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'); 
+2

昨天我遇到了同样的问题,它发生在分页查询中有一个组时。没有找到修复... – Dunhamzzz

回答

8

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

+0

谢谢!这几乎完全是我制定的 - 但这绝对是'全面解决'的解决方案。 –

+0

真棒解决方案,完美工作! – Benjam

+0

非常感谢很多:D – waseem

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']));添加到我的控制器,因此我可以访问分页参数。

0

功能paginateCount($条件= NULL,$递归= 0,$额外) {

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

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