How to split custom query into cakephp result

I am working on a cakephp project and new to cakephp. As mentioned in the header, I need to split the mysql query result set to display them in the view.

This way, I can paginate the results from the Asin model.

$this->paginate =array('Asin'=>array( 'fields' => array('sku','fnsku'), 'conditions' => $marketplace, 'page' => 1, 'limit' => 20, 'order' => array('Asin.asin' => 'asc') ) $data = $this->paginate('Asin',$criteria); 

And I also need a way to paginate the query results below.

 $resultset = $this->Asin->query("SELECT * FROM products INNER JOIN asins ON products.id=asins.id ORDER BY products.id"); 

How can I add pagination?

+5
source share
2 answers

CakePHP uses two methods for managing pagination requests, which are paginate and paginateCount, they are used to get page data and the total number of records, respectively. In order for pagination to be performed with your custom queries, we will need to implement both of the above functions in our model file, where you want to have pagination to work with custom queries.

 public function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) { $recursive = -1; // Mandatory to have $this->useTable = false; $sql = ''; $sql .= "Your custom query here just do not include limit portion"; // Adding LIMIT Clause $sql .= (($page - 1) * $limit) . ', ' . $limit; $results = $this->query($sql); return $results; } 

....

 public function paginateCount($conditions = null, $recursive = 0, $extra = array()) { $sql = ''; $sql .= "Your custom query here just do not include limit portion"; $this->recursive = $recursive; $results = $this->query($sql); return count($results); } 

Then, finally, in the action of the controller

 // Do not forgot to set this, not sure why $this->Asin->recursive = 0; // Setting up paging parameters $this->paginate = array('Asin'=>array('limit'=>5)); // Getting paginated result based on page # $this->set('userData', $this->paginate('Asin')); 
+4
source

If you use any association ship model, this is very simple http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

0
source

All Articles