NHTorres
NHTorres

Reputation: 1538

conditions OR in join cakephp

I am a novice in cake and I'm having trouble with a query I want to do in a tables.

        $options['joins'] = array(
        array('table'=>'users_views',
                  'alias' => 'UserView',
                  'type' => 'inner',
                  'conditions' => array(
                    'View.id = UserView.view_id',
                    'UserView.user_id' => $user_id,
                    'UserView.like' => 1
                   ),   
            )
        );
        $options['order'] = array('View.created' => 'desc');
        $options['limit'] = 10;

That's my query so far, but what I need now add an OR condition. In this condition OR need to add

'UserView.user_id' => $user_id,
'UserView.like' => 0

I need the query returns the data that matches or are mine and the LIKE value is 0 if it is very difficult to understand please tell me. Any help or advice will be well received.

Upvotes: 2

Views: 2083

Answers (2)

Sougata Bose
Sougata Bose

Reputation: 31749

Build the conditions like -

'conditions' => array(
     0 => array('View.id = UserView.view_id'),
     1 => array('UserView.user_id' => $user_id),
     3 => 'UserView.like => 1 OR UserView.like => 0'
),

Or with using OR -

'conditions' => array(
     0 => array('View.id = UserView.view_id'),
     1 => array('UserView.user_id' => $user_id),
     2 => 'UserView.like IN (0, 1)'
),

Upvotes: 2

Colonel Mustard
Colonel Mustard

Reputation: 1533

The following should work:

$options['joins'] = array(
            array('table'=>'users_views',
                'alias' => 'UserView',
                'type' => 'inner',
                'conditions' => array(
                    'View.id = UserView.view_id',
                    'OR' => array(
                        'UserView.user_id' => $user_id,
                        'UserView.like' => 1
                    )
                ),
            )
        );
        $options['order'] = array('View.created' => 'desc');
        $options['limit'] = 10;

Upvotes: 2

Related Questions