Cakephp Right Join Query


Cakephp Right Join Query : Right join basically pulls all rows records from the right table and only matching rows from left table. We pass the type parameter as “RIGHT” for Right joins. Here in this tutorial we are going to explain the RIGHT JOIN with example in cakephp framework. We will use cakephp model syntax for RIGHT JOIN in Cakephp.


Cakephp Right Join Query

Suppose we have a model called User which is associated with user table. Here in this example we are going to RIGHT JOIN this table with profile table which has user_id as foreign key.

Cakephp Right Join Query Example with syntax


Cakephp Right Join Query Example

   $userProfile = $this->User->find('all',
                array('joins' => array(
                                       array('table' => 'profile',
                                             'alias' => 'p',
                                             'type' => 'RIGHT',
                                             'foreignKey' => false,
                                             'conditions'=> array('user.id = p.user_id')                                             
                                        )
                                 ),
                 
                ));

The above example will RIGHT JOIN the two tables. it will generate the mysql query something like this –

Query Generated : SELECT user.id, user.name, user.email, user.phone FROM user RIGHT JOIN p on (user.id = p.user_id)

Advertisements

Add Comment

📖 Read More