Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination not working (Cake 3.6.7) #26

Open
minlite opened this issue Jan 6, 2019 · 2 comments
Open

Pagination not working (Cake 3.6.7) #26

minlite opened this issue Jan 6, 2019 · 2 comments

Comments

@minlite
Copy link

minlite commented Jan 6, 2019

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!

@rajjecrc
Copy link

rajjecrc commented Sep 11, 2019

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants