When paginating results that use GROUP BY in CakePHP, the # of results count is incorrect. This is a documented item as is (was) to be expected, but according to this page in the CakePHP book:
In CakePHP 2.0, you no longer need to implement paginateCount() when
using group clauses. The core find(‘count’) will correctly count the
total number of rows.
I’m using CakePHP 2.2.3, yet I still have the problem. Is there something specific I need to do? Am I misreading the statement? Do I still need to use custom paginate() and/or paginateCount methods?
Clarification of “incorrect count”: I’m joining table1 w/ table2 – so even though I only have one “table1 item”, it retrieves 2 rows… but then I group by to the id of the table1 item, which makes it return only 1 item. The count, however, shows “2 found”, but only displays 1 item.
Update: (code)
$this->Paginator->settings = array(
'limit' => (int) 20,
'conditions' => array(
(int) 0 => array(
'Article.node_type_id' => '5050ede8-3f88-45f4-b58f-1130d9d84497'
),
(int) 1 => array(
'OR' => array(
'DataText.title LIKE' => '%john%',
'Article.name LIKE' => '%john%'
)
)
),
'order' => array(
'Article.created' => 'DESC'
),
'fields' => array(),
'joins' => array(
(int) 0 => array(
'table' => 'data_texts',
'alias' => 'DataText',
'type' => 'INNER',
'conditions' => array(
(int) 0 => 'DataText.node_id = Article.id'
)
)
),
'contain' => array(
'Slug' => array(
'order' => array(
(int) 0 => 'FIELD(Slug.language_id, "c141eafd-567a-4bc5-badd-c5a163c01f46") DESC'
)
),
'NodeType' => array(
'ChildNodeType' => array()
),
'DataLocation' => array(
'conditions' => array()
),
'DataMeta' => array(
'conditions' => array()
)
),
'group' => 'Article.id'
)
Paginate call itself:
$nodes = $this->paginate($model);
There was a bug in
find("count")and it returned incorrect count if the query resulted in records for only 1 group. This has been fixed now. The fix was probably done after 2.2.3 release so just use themasterbranch from github for now and the fix will be available in the next release.