mongodb-cakephp3 icon indicating copy to clipboard operation
mongodb-cakephp3 copied to clipboard

Pagination not working (Cake 3.6.7)

Open minlite opened this issue 6 years ago • 2 comments

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!

minlite avatar Jan 06 '19 22:01 minlite

many many thanks @minlite i was facing this issue, than somehow found your answer. i am also facing buildrule check issue below function is not working , can you help me? public function buildRules(RulesChecker $rules) { $rules->add($rules->isUnique(['email'])); return $rules; } }

rajjecrc avatar Sep 11 '19 07:09 rajjecrc

also pagination function is not working correctly while applying filter. like i searching a name 'raj' and found 1 record than also pagination showing wrong pages and next,previous buttons CakePHP  the rapid development php framework  Employees

rajjecrc avatar Sep 11 '19 09:09 rajjecrc