CakePHP 2.x: Paging with Custom Query

1

I've hit my head so far to try to understand how CakePHP's pagination works with Custom Query's but I have not had much success. This is the method I currently have, I want to make a paging on top of it, how to use Paginator in this situation?

    function search(){
    $this->layout = 'mpf';
    $fields = array('Convenio.*','tbl_agencias.gerev');
    $join = array(  
        array(
            'table' => 'tbl_agencias',
                'type' => 'LEFT',
                'conditions' => array('Convenio.centralizadora = tbl_agencias.id')
        )
    );
    $conditions = array(
        'OR' => array(
            'Convenio.nome LIKE' => "%".$this->request->data['Convenio']['search']."%",
            'Convenio.centralizadora' => $this->request->data['Convenio']['search'],
            'Convenio.id' => $this->request->data['Convenio']['search'],
        ),
        array(
            'AND' => array(
                'Convenio.situacao != ' => '-1'
            )
        )
    );
    $group = array('Convenio.nome');
    $this->set('convenios', $this->Convenio->find('all',array('fields'=>$fields,'conditions'=>$conditions,'joins'=>$join,'group'=>$group)));
}
    
asked by anonymous 15.08.2015 / 03:47

2 answers

0

I think it was just lack of interpretation. It was enough to pass the parameters as if it were in find() itself

It looks like this:

   function search(){
        $this->layout = 'mpf';
        $fields = array('Convenio.*','tbl_agencias.gerev');
        $join = array(  
            array(
                'table' => 'tbl_agencias',
                    'type' => 'LEFT',
                    'conditions' => array('Convenio.centralizadora = tbl_agencias.id')
            )
        );
        $conditions = array(
            'OR' => array(
                'Convenio.nome LIKE' => "%".$this->request->data['Convenio']['search']."%",
                'Convenio.centralizadora' => $this->request->data['Convenio']['search'],
                'Convenio.id' => $this->request->data['Convenio']['search'],
            ),
            array(
                'AND' => array(
                    'Convenio.situacao != ' => '-1'
                )
            )
        );
        $group = array('Convenio.nome');
        $this->Paginator->settings = array(
            'conditions' => $conditions,
            'fields' => $fields,
            'joins' => $join,
            'group' => $group,
            'limit' => 25
        );
        $convenios = $this->Paginator->paginate('Convenio');
        $this->set(compact('convenios'));
        //$this->set('convenios', $this->Convenio->find('all',array('fields'=>$fields,'conditions'=>$conditions,'joins'=>$join,'group'=>$group)));
    }
    
15.08.2015 / 18:12
0

GitHub - Practical example of custom find with cakephp

Dear AComerlatto, I hope it helps, this example is one of the many methods written to meet more elaborate needs.

    
26.09.2015 / 03:45