10
votes

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'][].

4
There is a solution for this here: CakePHP 2.3.4 stackoverflow.com/questions/13645296/…Eskil Mjelva Saatvedt

4 Answers

15
votes

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.

2
votes

I'm not sure if you need those [] - try just doing this:

$this->paginate['conditions']['Reservation.pagamento_verificato'] = 'SI';
1
votes

I use the conditions when I call paginate method.

$this->paginate($conditions)

This works ok for me, I hope it works for you!

If you have setted previous params, you may use:

$this->paginate(null,$conditions)
1
votes

This might be help full to someone.... This is how I did complicated joins with pagination in cakephp.

$parsedConditions['`Assessment`.`showme`'] = 1;
$parsedConditions['`Assessment`.`recruiter_id`'] = $user_id;

$this->paginate = array(
'conditions' => array($parsedConditions ),
  'joins' => array(
    array(
        'alias' => 'UserTest',
        'table' => 'user_tests',
        'type' => 'LEFT',
        'conditions' => '`UserTest`.`user_id` = `Assessment`.`testuser_id`'
    ),
    array(
        'alias' => 'RecruiterUser',
        'table' => 'users',
        'type' => 'LEFT',
        'conditions' => '`Assessment`.`recruiter_id` = `RecruiterUser`.`id`'
    )
    ,
    array(
        'alias' => 'OwnerUser',
        'table' => 'users',
        'type' => 'LEFT',
        'conditions' => '`Assessment`.`owner_id` = `OwnerUser`.`id`'
    )
),
  'fields' => array('Assessment.id', 'Assessment.recruiter_id', 'Assessment.owner_id', 'Assessment.master_id', 'Assessment.title', 'Assessment.status', 'Assessment.setuptype','Assessment.linkkey', 'Assessment.review', 'Assessment.testuser_email', 'Assessment.counttype_2', 'Assessment.bookedtime', 'Assessment.textqstatus', 'Assessment.overallperc', 'UserTest.user_id', 'UserTest.fname', 'UserTest.lname', 'RecruiterUser.company_name', 'OwnerUser.company_name'),
  'limit' => $limit,
        'order'=>   array('Assessment.endtime' => 'desc')   
    );