Custom Model Error Pagination

Hi everyone…

Thanks for letting me joining this forum…

I’ve bought grocery crud enterprise, and for basic needs, it suits me well and help me alot. Problem arise when i want to use some custom query and use set model. Despite of the table show up sucessfully, the search and the pagination didnt work. it only show 10, 20 or 50 first data (depend on the setting per page). I am using the code example as show on github here

i am inserting code that i use here…i am appreciate for the help… and sorry for my bad english.

<?php defined('BASEPATH') OR exit('No direct script access allowed'); 
use GroceryCrud\Core\Model; 
use GroceryCrud\Core\Model\ModelFieldType;  

class M_custombaranggc extends Model
{  	
protected $ci; 	
protected $db; 	
protected $limit = 10;  	
function __construct($databaseConfig) 
{ 		
$this->setDatabaseConnection($databaseConfig);  		
$this->ci = & get_instance(); 		
$this->db = $this->ci->db; 	
}  	

public function getFieldTypes($tableName) 	
{ 		
$fieldTypes = parent::getFieldTypes($tableName);  		
$tahunPerolehanFieldType = new ModelFieldType(); 		
$tahunPerolehanFieldType->dataType = 'varchar';  		
$uraianbarangFieldType = new ModelFieldType(); 		
$uraianbarangFieldType->dataType = 'varchar';  		
$fieldTypes['tahunperolehan'] = $tahunPerolehanFieldType; 		
$fieldTypes['uraianbarang'] = $uraianbarangFieldType;  		
return $fieldTypes; 	
}  	

protected function _getQueryModelObject() { 				
// All the custom stuff here 		
$this->db->select('baranggc.id, baranggc.kd_lokasi, baranggc.kd_brg, baranggc.merk_type as merk_type, t_subkelompok.ur_sskel as uraianbarang, baranggc.no_aset, year(baranggc.tgl_perlh) as tahunperolehan, 
if(tercatat=1,"DBR",if(tercatat=2,"DBL","KIB")) as tercatat,
if(kondisi=1,"Baik",if(kondisi=2,"Rusak Ringan","Rusak Berat")) as kondisi,  							
if(flag_sap="y","Intra","Ekstra") as "flag_sap" ', false); 		
$this->db->join('t_subkelompok', 't_subkelompok.kd_brg = baranggc.kd_brg', 'left');  		

if (!empty($this->_filters)) { 			
foreach ($this->_filters as $filter_name => $filter_value) { 				
if ($filter_name === 'uraianbarang') { 
if (is_array($filter_value)) 
{ 						
foreach ($filter_value as $value) 
{ 							
$this->db->like('t_subkelompok.ur_sskel', $value); 						
} 					
} else { 						
$this->db->like('t_subkelompok.ur_sskel', $filter_value); 					
}  				
} else { 					
if (is_array($filter_value)) { 						
foreach ($filter_value as $value)
{ 							
$this->db->like($filter_name, $value); 						
} 					
} else { 						
$this->db->like($filter_name, $filter_value); 					
} 				
} 			
} 		
}  		
$this->db->limit($this->limit); 		
$this->db->offset($this->limit * ($this->page - 1)); 		
$order_by = $this->orderBy; 		
$sorting = $this->sorting; 		
if ($order_by !== null) { 			
$this->db->order_by($order_by. " " . $sorting); 		
} 		
return $this->db->get($this->tableName); 	
}  	

public function getList() {  		
return $this->_getQueryModelObject()->result_array(); 	
}  	

public function getTotalItems() 	{ 		

if (!empty($this->_filters)) { 			
return $this->_getQueryModelObject()->num_rows(); 		
}  		

// If we don't have any filtering it is faster to have the default total items 		// In case this is more complicated you can add your own code here 		

return parent::getTotalItems(); 	}  } 

Hello @sam210388 and welcome to our forums :hugs: ,

You are right! There was an issue at the documentation for the limitation that I’ve never thought about. I’ve updated also the documentation for customModel. Below you can find the diff that I’ve updated so it can also include the limitation bug that you are referring to.

From 9a098d01062445dd641261c14ff8f0e03496a6f1 Mon Sep 17 00:00:00 2001
From: John Skoubourdis
Date: Sun, 19 Sep 2021 21:02:01 +0300
Subject: [PATCH] Fixing small issue with custom model and filtering that does
 also limits the grid

---
 2.x/advanced-examples/custom-model.md | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/2.x/advanced-examples/custom-model.md b/2.x/advanced-examples/custom-model.md
index badb625..0e45dd7 100644
--- a/2.x/advanced-examples/custom-model.md
+++ b/2.x/advanced-examples/custom-model.md
@@ -68,7 +68,7 @@ class CustomersModel extends Model {
         return $customer;
     }
 
-    protected function _getQueryModelObject() {
+    protected function _getQueryModelObject($withLimit = true) {
         $order_by = $this->orderBy;
         $sorting = $this->sorting;
 
@@ -109,7 +109,10 @@ class CustomersModel extends Model {
             }
         }
 
-        $this->db->limit($this->limit, ($this->limit * ($this->page - 1)));
+        if ($withLimit) {
+            $this->db->limit($this->limit, ($this->limit * ($this->page - 1)));
+        }
+        
         return $this->db->get($this->tableName);
     }
 
@@ -120,7 +123,7 @@ class CustomersModel extends Model {
     public function getTotalItems()
     {
         if (!empty($this->_filters)) {
-            return $this->_getQueryModelObject()->num_rows();
+            return $this->_getQueryModelObject(false)->num_rows();
         }
 
         // If we don't have any filtering it is faster to have the default total items
-- 
2.23.0

I hope this helped.

Regards
Johnny

thanks johnny… it works charmly… i’ve used it in my application… thanks a lot man…

1 Like