-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Bug: in-model pager ignores the model event (with where clause, run via callback), reporting incorrect paging information #8412
Comments
You reset the query with echo 'total number of entries in table: ' . $model->countAllResults(); Try |
Tried; it changes nothing. Updated the test code above. And thanks for code colouring :) |
Ah, |
I assumed that, no problem here. It is the results at this line in output:
and later that are the problem. Pager counts the number of results that the model returns wrong. |
My theory of this error after looking in the code of The The method $this->pager = $pager->store($group, $page, $perPage, $this->countAllResults(false), $segment); How does that sound? |
It seems that we cannot solve this issue easily. When we generate pagination, we need to:
Also: If we call So the query order is not wrong in the current code: CodeIgniter4/system/BaseModel.php Lines 1228 to 1244 in 4308c0b
|
I don't know how to fix this issue or if we should not fix it. Workaround is to override public function paginate(?int $perPage = null, string $group = 'default', ?int $page = null, int $segment = 0)
{
// Fire restrictToUser() manually. // Added
$this->restrictToUser([]); // Added
// Since multiple models may use the Pager, the Pager must be shared.
$pager = Services::pager();
if ($segment !== 0) {
$pager->setSegment($segment, $group);
}
$page = $page >= 1 ? $page : $pager->getCurrentPage($group);
// Store it in the Pager library, so it can be paginated in the views.
$this->pager = $pager->store($group, $page, $perPage, $this->countAllResults(false), $segment);
$perPage = $this->pager->getPerPage($group);
$offset = ($pager->getCurrentPage($group) - 1) * $perPage;
// Disable model events for findAll(). // Added
$this->allowCallbacks(false); // Added
return $this->findAll($perPage, $offset);
} |
I see; I was looking at the code yesterday and also could not envisage a way to improve it (but thought maybe the more experienced people might). The suggested workaround would work I guess while I only have that single callback; either that or just add the needed where clause manually before the call to pagination (as I do now; it does generate a double WHERE but it works). I am leaving the issue open, but I am ok with it being closed as WONTFIX if an efficient solution cannot be found. Thanks for investigating, @kenjis! |
I wouldn't say it's a bug. Documentation is clear, what methods are affected by the database events: https://codeigniter.com/user_guide/models/model.html#event-parameters Although this may be inconvenient, just like in the presented case. To make it work, we would have to modify the |
According to the current documentation, it is not a bug. The behavior is expected. My question is: should |
The events only work on model-specific methods and countAllResults is from the query builder. I understand that this is a weird case but no I don't think so. The events were never intended to change the number of results, only modify the results that were found. Filtering should happen after results are returned. |
@lonnieezell Model has Line 611 in 4308c0b
Do you say the following Model callback is kind of a misuse? protected function restrictToUser(array $data)
{
// in my case the user_id comes from session, hardcode here:
$this->where($this->table . '.user_id', 3);
return $data;
} |
Ah, I forgot that was overridden for soft-deletes. Seems I misspoke. Would I say it's a misuse? No, just not what events were originally thought of. Everything always gets used in creative ways you didn't expect. Part of the fun - seeing how others use your stuff. :) Thinking of it another way, though, no I wouldn't expect a |
PHP Version
8.1
CodeIgniter4 Version
4.4.4
CodeIgniter4 Installation Method
Composer (using
codeigniter4/appstarter
)Which operating systems have you tested for this bug?
Linux
Which server did you use?
cli-server (PHP built-in webserver)
Database
SQLite3
What happened?
I have a custom event in a model that applies where clause to every search implemented thus:
protected $beforeFind = ['restrictToUser'];
When I use it with an in-model pager, the find results apply this event, but the pager does not, resulting in wrong page count and wrong total record number.
Steps to Reproduce
Start with empty project.
Rename
env
to.env
, change these lines in it to the values:Add a cli route in app/Config/Routes.php:
$routes->cli('home', 'Home::index');
Add migration for a table:
php spark make:migration CreateTestTable
Replace up() and down() methods with:
make a seeder:
php spark make:seeder test --suffix
Replace the seeder method run() with this:
Create model:
php spark make:model TestModel
in model, update the $beforeFind callback thus:
protected $beforeFind = ['restrictToUser'];
and add event method to model:
Add test code to the Home controller, replace the index() method with:
Run the test :
Expected Output
I expect the in-model pager report the same number of entries as returned by the model paginate() method, applying the in-model specified event correctly.
Anything else?
No response
The text was updated successfully, but these errors were encountered: