Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cakephp $this->paginate with custom JOIN and filtering options

Tags:

cakephp

I've been working with cakephp paginations options for 2 days. I need to make a INNER Joins to list a few fields, but I have to deal with search to filter results. This is portion of code in which I deal with search options by $this->passedArgs

function crediti() {

    if(isset($this->passedArgs['Search.cognome'])) {
                debug($this->passedArgs);

                $this->paginate['conditions'][]['Member.cognome LIKE'] = str_replace('*','%',$this->passedArgs['Search.cognome']);

        }
        if(isset($this->passedArgs['Search.nome'])) {
                $this->paginate['conditions'][]['Member.nome LIKE'] = str_replace('*','%',$this->passedArgs['Search.nome']);

        }

and after

$this->paginate = array(

            'joins' => array(array('table'=> 'reservations',
            'type' => 'INNER',
            'alias' => 'Reservation',
            'conditions' => array('Reservation.member_id = Member.id','Member.totcrediti > 0' ))),
            'limit' => 10);
        $this->Member->recursive = -1;
        $this->paginate['conditions'][]['Reservation.pagamento_verificato'] = 'SI';
        $this->paginate['fields'] = array('DISTINCT Member.id','Member.nome','Member.cognome','Member.totcrediti');
        $members = $this->paginate('Member');
        $this->set(compact('members'));

INNER JOIN works good, but $this->paginations ignore every $this->paginate['conditions'][] by $this->passedArgs and I cannot have idea how I can work it out. No query in debug, just the original INNER JOIN. Can someone helps me ? Thank you very much

Update: No luck about it. I've been dealing with this part of code for many hours. If I use

if(isset($this->passedArgs['Search.cognome'])) {
                    $this->paginate['conditions'][]['Member.cognome LIKE'] = str_replace('*','%',$this->passedArgs['Search.cognome']);

            }
$this->paginate['conditions'][]['Member.sospeso'] = 'SI';
        $this->Member->recursive = 0;
        $this->paginate['fields'] = array(
            'Member.id','Member.nome','Member.cognome','Member.codice_fiscale','Member.sesso','Member.region_id',
            'Member.district_id','Member.city_id','Member.date','Member.sospeso','Region.name','District.name','City.name');
        $sospesi = $this->paginate('Member');

everything goes well, and from debug I receive the first condition and the conditions from $this->paginate['conditions'][]['Member.cognome LIKE'], as you can see array $this->passedArgs

Array
(
    [Search.cognome] => aiello
)

Array $this->paginate['conditions'][]
(
    [0] => Array
        (
            [Member.cognome LIKE] => aiello
        )

    [1] => Array
        (
            [Member.sospeso] => NO
        )

But, if I write the joins with paginate , $this->paginate['conditions'][] will ignore all the stuff, and give me from debug, just $this->paginate['conditions'][]['Reservation.pagamento_verificato'] = 'SI'; Another bit of information. If I put all the stuff dealing with $this->paginate['conditions'][]['Reservation.pagamento_verificato'] = 'SI'; before the $this->paginate JOIN, nothing will be in $this->paginate['conditions'][].

like image 491
n4than Avatar asked Apr 20 '11 10:04

n4than


1 Answers

This is an old question, so I'll just review how to do a JOIN in a paginate for others who got here from Google like I did. Here's the sample code from the Widget's Controller, joining a Widget.user_id FK to a User.id column, only showing the current user (in conditions):

// Limit widgets shown to only those owned by the user.
$this->paginate = array(
    'conditions' => array('User.id' => $this->Auth->user('id')),
    'joins' => array(
        array(
            'alias' => 'User',
            'table' => 'users',
            'type' => 'INNER',
            'conditions' => '`User`.`id` = `Widget`.`user_id`'
        )
    ),
    'limit' => 20,
    'order' => array(
        'created' => 'desc'
    )
);
$this->set( 'widgets', $this->paginate( $this->Widget ) );

This makes a query similar to:

SELECT widgets.* FROM widgets 
INNER JOIN users ON widgets.user_id = users.id 
WHERE users.id = {current user id}

And still paginates.

like image 59
Dave Lancea Avatar answered Oct 27 '22 15:10

Dave Lancea