When using CakePHP 3.6.7, the pagination function is not working. It always seems to think that the total row count for a query is 1, therefore the pagination component sets page count to 1.
I've traced this issue down to the fact that that CakePHP pagination module has the following code:
cakephp/src/Datasource/Paginator.php (188):
$count = $cleanQuery->count();
....
$pageCount = max((int)ceil($count / $limit), 1);
$page = min($page, $pageCount);
Here, query is an instance of MongoQuery. The $count seems to be always set to 1, no matter how many records there are in the database.
I was able to fix this temporarily by changing the following in src/ORM/Table.php:
public function find($type = 'all', $options = [])
{
$query = new MongoFinder($this->__getCollection(), $options);
$queryWoOptions = new MongoFinder($this->__getCollection(), []);
$method = 'find' . ucfirst($type);
if (method_exists($query, $method)) {
$alias = $this->getAlias();
$mongoCursor = $query->{$method}();
$mongoCursorWoOptions = $queryWoOptions->{$method}();
if ($mongoCursor instanceof \MongoDB\Model\BSONDocument) {
return (new Document($mongoCursor, $alias))->cakefy();
} elseif (is_array($mongoCursor)) {
return $mongoCursor;
}
$results = new ResultSet($mongoCursor, $alias);
$resultsWoOptions = new ResultSet($mongoCursorWoOptions, $alias);
if (isset($options['whitelist'])) {
return new MongoQuery($results->toArray(), count($resultsWoOptions->toArray()));
} else {
return $results->toArray();
}
}
throw new BadMethodCallException(
sprintf('Unknown method "%s"', $method)
);
}
(Notice the addition of $queryWoOptions, $mongoCursorWoOptions, and $resultsWoOptions )
I will try to track this down more, but just wanted to open an issue to see if the developers might have an idea about this issue.
Thanks!
When using CakePHP 3.6.7, the pagination function is not working. It always seems to think that the total row count for a query is 1, therefore the pagination component sets page count to 1.
I've traced this issue down to the fact that that CakePHP pagination module has the following code:
cakephp/src/Datasource/Paginator.php (188):
Here, query is an instance of MongoQuery. The
$countseems to be always set to 1, no matter how many records there are in the database.I was able to fix this temporarily by changing the following in src/ORM/Table.php:
(Notice the addition of
$queryWoOptions,$mongoCursorWoOptions, and$resultsWoOptions)I will try to track this down more, but just wanted to open an issue to see if the developers might have an idea about this issue.
Thanks!