Note that there are some explanatory texts on larger screens.

plurals
  1. POCakephp custom query pagination
    primarykey
    data
    text
    <p>I'm trying to use custom query in Cake then paginate the results with the code below:</p> <pre><code>$query = $this-&gt;Petition-&gt;query($sql); </code></pre> <p>I tried:</p> <pre><code>$petitions = $this-&gt;paginate($query); </code></pre> <p>and it doesn't work. Is there a way we can do this?</p> <p>OK I wasn't clear enough: I need to use variable array fetched from custom query on pagination so I can use this for pagination in the view. Is there an easy way of doing this? Below is my code:</p> <pre><code>function index() { if ($this-&gt;Session-&gt;read('Auth.User.group_id') != 1) { $commune_id = $this-&gt;Session-&gt;read('Auth.User.commune_id'); $commune_id = $this-&gt;Petition-&gt;Commune-&gt;findbyId($commune_id); $commune_id = $this-&gt;Petition-&gt;Commune-&gt;find('all',array('conditions' =&gt; array('group' =&gt; $commune_id['Commune']['group']))); $count = count($commune_id); $i=1; $sql = "SELECT * FROM `petitions` WHERE `commune_id` = "; foreach($commune_id as $commune_ids){ if($i==1){ $sql .= $commune_ids['Commune']['id']; }else{ $sql .= " OR `commune_id` = ".$commune_ids['Commune']['id']; } /*if($i != $count){ $this-&gt;paginate = array( 'or' =&gt; array( array('Petition.commune_id LIKE' =&gt; $commune_ids['Commune']['id'] . ","), //array('Petition.commune_id LIKE' =&gt; "," . $commune_ids['Commune']['id'] . ",") ), 'limit' =&gt; 10 ); }*/ $i++; } $query = $this-&gt;Petition-&gt;query($sql); } $this-&gt;Petition-&gt;recursive = 0; $petitions = $this-&gt;paginate(); $this-&gt;set('petitions', $petitions); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload