Welcome Guest, Not a member yet? Register   Sign In
Change the paginate method
#1

Hi! Since the manual numbering of pages in the method  $ pager->makeLinks ($page, $perPage, $total) is $total,
I propose to change the paginate method in the System/Model.php:

PHP Code:
public function paginate(int $perPage 20string $group 'default'int $page 0,bool return_with_total false)
    {
        
// Get the necessary parts.
        
$page $page >= $page : (ctype_digit($_GET['page'] ?? '') && $_GET['page'] > $_GET['page'] : 1);

        
$total $this->countAllResults(false);

        
// Store it in the Pager library so it can be
        // paginated in the views.
        
$pager       = \Config\Services::pager();
        
$this->pager $pager->store($group$page$perPage$total);

        
$offset = ($page 1) * $perPage;

        if(
return_with_total)return ['total'=>$total,'table' => $this->findAll($perPage$offset)]; // Add

        
return $this->findAll($perPage$offset);
    } 
This will avoid unnecessary duplicate requests.

Code:
2019-05-25T08:55:27.306774Z       33 Query    SELECT COUNT(*) AS `numrows`
FROM `customer`
WHERE `id` IN (1,2,3,4,5)
AND `name` LIKE '%test%'
2019-05-25T08:55:27.307196Z       33 Query    SELECT COUNT(*) AS `numrows`
FROM `customer`
WHERE `id` IN (1,2,3,4,5)
AND `name` LIKE '%test%'
2019-05-25T08:55:27.309970Z       33 Query    SELECT *
FROM `customer`
WHERE `id` IN (1,2,3,4,5)
AND `name` LIKE '%test%'
ORDER BY `id` ASC
LIMIT 10
2019-05-25T08:55:27.324738Z       33 Quit
Reply
#2

Paginate acts as findAll with auto applied limit + offset and prepared Pager..

I think that logically countAllResults(false) should preserve the count once it's done and just to return it instead making multiple selects further..
https://github.com/codeigniter4/CodeIgni....php#L1566
Running that query multiple times in case $sql and $this->binds are the same is pointless specially if $reset is set to false..
Best VPS Hosting : Digital Ocean
Reply
#3

You know better! But now the following code accesses the database 3 times and this is not correct:
PHP Code:
$segment  3
$perPage  10;
$page       = (int)$params[0];
if(!
$page)$page++;
$template 'default'
$model = new CustomerModel();
$validation = \Config\Services::validation();
$validation->setRules([
        
'name'      => ['label' => 'Name''rules' => 'required|max_length[50]'],
        
'email'  => ['label' => 'e-mail''rules' => 'required|max_length[100]'],
$model->where('active','yes');

$total $model->countAllResults(false);

$data   
 
   'table' =>  $model->paginate($perPage,$template,$page),
    
'pagerlinks' =>  $model->pager->Makelinks($page,$perPage,$total,$template.'_full',$segment)
    ]; 

The first call to the COUNT database: $total = $model->countAllResults(false);
The second two calls to the COUNT and * DB: $model->paginate($perPage,$template,$page)
Reply




Theme © iAndrew 2016 - Forum software by © MyBB