Creating left joins in cakephp - cakephp

I am trying to select data using LEFT join in cakephp, here how can i use more than two tables for LEFT join.Here iam creating LEFT join between two tables news_feeds and news_feed_likes.i want to introduce two more tables here, NewsFeedComments and newsfeed_likes_comments.How can i do this?
Thanks for your help
$this->paginate = array(
'conditions' => array('NewsFeed.group_id'=>$groupdata['Group']['id'],'NewsFeed.status'=>'A'),
'joins' => array(
array(
'alias' => 'newslikes',
'table' => 'news_feed_likes',
'type' => 'LEFT',
'conditions' => array(
'newslikes.news_feed_id = NewsFeed.id',
'newslikes.user_id'=>$this->Auth->user('id'),
'newslikes.status'=>'1',
),
),
array(
'alias' => 'newscommentslikes',
'table' => 'news_feed_comments_likes',
'type' => 'LEFT',
'conditions' => array(
'newscommentslikes.news_feed_comment_id = NewsFeedComment.id',
'newscommentslikes.user_id'=>$this->Auth->user('id'),
'newscommentslikes.status'=>'1',
),
),
),
'fields' => array(
'IFNULL(newslikes.user_id,0) AS likestatus',
'NewsFeed.id',
'NewsFeed.posted_message',
'NewsFeed.created',
'NewsFeed.user_id',
'NewsFeed.status',
'NewsFeed.newslike',
'IFNULL(newslikes.status,0) AS status',
),
'order' => array('NewsFeed.created' => 'desc'),
);
$this->set('newsfeed', $this->paginate( $this->NewsFeed ) );

My first bit of advice would be to consider moving this to the the model. However, to answer the specific question, just add more joins based on the foreign key. So, for example, if you have a foreign key between NewsFeedLike and NewsFeedLikeComments, you can just add another join like so:
'joins' => array(
array(
'table' => 'news_feed_like_comments',
'alias' => 'NewsFeedLikeComment',
'type' => 'LEFT',
'conditions' => array(
'NewsFeedLikeComment.news_feed_like_id = NewsFeedLike.id',
),
),
Because you are already joining NewsFeedLike, it will use those conditions to limit the NewsFeedLikeComment.
Another tip: Always follow the CakePHP coding standards when writing your code. It will make your life a lot less painful. Avoid using lowercase underscored aliases for your models. Always write them CamelCase with a capital first letter and singular. So in your joins, instead of newslikes and newscommentslikes as aliases, you should use NewsLike and NewsCommentLike.

You can join multiple tables using below syntax:
$this->paginate = array(
'conditions' => array(),
'joins' => array(
array(
'alias' => 'TableAlias1',
'table' => 'table_name_1',
'type' => 'LEFT',
'conditions' => array(
'TableName.id = table_name_1.id',
),
),
array(
'alias' => 'TableAlias2',
'table' => 'table_name_2',
'type' => 'LEFT',
'conditions' => array(
'TableName.id = table_name_2.id',
),
),
array(
'alias' => 'TableAlias3',
'table' => 'table_name_3',
'type' => 'LEFT',
'conditions' => array(
'TableName.id = table_name_4.id',
),
)
),
'fields' => array(),
'order' => array()
);

Related

How to get associated data of a model using custom join cakephp2

I am using cakephp2 and trying to join two tables like this :
$employee=$this -> Employee ->find ('all',array(
'joins'=>array(
array('table' => 'employee_histories', 'alias' => 'eh', 'type' => 'LEFT', 'foreignKey' => false,
'conditions' => array('eh.emp_id= Employee.emp_id'))
),
'fields'=>'Employee.*,eh.*',
));
In EmployeeHistory model there is an association with table employee_designations like this :
public $belongsTo = array(
'EmployeeDesignation' => array(
'className' => 'EmployeeDesignation',
'foreignKey' => '',
'conditions' => 'EmployeeDesignation.designation_id=EmployeeHistory.designation_id',
'fields' => '',
'order' => ''
),
);
Now is it possible to get the EmployeeDesignation result in $employee array.
You can join the employee_designations table by adding another array to your joins array. Something like this:
$employee= $this->Employee->find('all', array(
'joins'=>array(
array(
'table' => 'employee_histories',
'alias' => 'eh',
'type' => 'LEFT',
'foreignKey' => false,
'conditions' => array(
'eh.emp_id= Employee.emp_id'
)
),
// array joining the employee designations table
array(
'table' => 'employee_designations',
'alias' => 'ed',
'type' => 'LEFT',
'conditions' => array(
'eh.designation_id = ed.designation_id'
)
)
),
'fields'=>'Employee.*, eh.*, ed.*', // include ed fields
));

Join and Count Tables in cakePHP

thanks to the help i have gotten on Stackoverflow while learning php i am able to Join tables and use Count.
I am unable to do both in one query.
I am wanting to count records in a joined table.
This is what i have tryed and i seem to get errors:
$options = array(
'fields' => array(
'toutcome.AffCommission',
),
'joins' => array(
array(
'conditions' => array(
'tapplicant.AppID = toutcome.AppID',
),
'table' => 'toutcome',
'alias' => 'Toutcome',
'type' => 'join',
),
),
'limit' => 'Toutcome',
'offset' => 'Toutcome',
'contain' => array(
'Toutcome',
),
);
$data = $this->Tapplicant->find('count', $options);
$this->set('count', $data );
Try this
$options = array(
'fields' => array(
'toutcome.AffCommission',
),
'joins' => array(
array(
'conditions' => array(
'tapplicant.AppID = toutcome.AppID',
),
'table' => 'toutcome',
'alias' => 'Toutcome',
'type' => 'join',
),
),
'limit' => n, // its should be integer
'offset' => n, // its should be integer
'contain' => array(
'Toutcome',
),
);

CakePHP join only the latest entry

I have 2 tables :
ORDERS
------
id
type
LOGS
----
id
order_id
time
I want to query with cakephp so I have :
array(
'Order' => array(
'id' => '38',
'type' => 'online',
),
'Log' => array(
'time' => '2014-09-24 21:17:14'
)
)
The problem is that I want only the last order, not all orders with all logs.
I did something like this :
$ordersList = $this->Order->find('all', array(
'fields' => array(
'Order.*',
'Log.time'
),
'joins' => array(
array(
'table' => 'logs',
'alias' => 'Log',
'type' => 'right',
'conditions' => array(
'Log.order_id = Order.id'
),
)
),
);
To have only the last one, you can order the result by log.time and then take only the first record (with the param 'first' or just by fetching the first record of the recordset).
For example :
$order = $this->Order->find('first', array(
'order' => array('Log.time' => 'desc')
));
in your case :
$ordersList = $this->Order->find('first', array(
'fields' => array(
'Order.*',
'Log.time'
),
'joins' => array(
array(
'table' => 'logs',
'alias' => 'Log',
'type' => 'right',
'conditions' => array(
'Log.order_id = Order.id'
),
'order' => array('Log.time' => 'desc')
)
),
);
As information, if you want to have a simple method to join the models, take a look at the containable behavior. With this behavior, the link are setted in the model then you only have to declare which associated model you want to retrieve with your current model.
=> http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html

cakephp paginate with associated model

User has many User_Questionaries. I want paginate users that have particular questionnaire. I used following pagination for it.
$paginate = array(
'conditions' => array(
'User.role' => IWOA,
'UserQuestionary.questionary_id' => $id
),
'recursive' => 1,
'limit' => 10,
'order' => array(
'name' => 'asc'
),
'contain' => array('UserQuestionary')
);
But it is not create join query. It is showing Unknown column UserQuestionary.questionary_id' in 'where clause'
What is the issue? How can i do it?
Finally I used join query for do this.
$paginate = array(
'conditions' => array(
'Iwoa.role' => IWOA,
),
'joins' => array(
array(
'alias' => 'UserQuestionary',
'table' => 'user_questionaries',
'type' => 'LEFT',
'conditions' => 'UserQuestionary.user_id = Iwoa.id AND UserQuestionary.questionary_id = ' . $id
)
),
'limit' => 10,
'order' => array(
'name' => 'asc'
),
);

CakePHP join issue

This is driving me crazy. This is not throwing any errors but it is also not performing the joins. I'm hoping that this is one where I've spent too long looking at it and the answer is obvious to someone else...
$lines = $this->RevenueLine->find('all', array(
'conditions' => array(
'RevenueLine.is_triggered' => 1,
'RevenueLine.date_triggered >=' => $sqldate1,
'RevenueLine.date_triggered <=' => $sqldate2,
),
'joins' => array(
array(
'table' => 'projects',
'alias' => 'Project',
'type' => 'INNER',
'conditions' => array(
'RevenueLine.project_id = Project.id'
)
),
array(
'table' => 'clients',
'alias' => 'Client',
'type' => 'INNER',
'conditions' => array(
'Project.client_id = Client.id'
)
),
array(
'table' => 'classifications',
'alias' => 'Classification',
'type' => 'INNER',
'conditions' => array(
'Project.classification_id = Classification.id'
)
)
),
'order' => array(
'Client.client_number ASC',
'Project.pn_counter ASC'
)
)
);
You need to select the fields from the joined tables:
'fields' => array(
'JoinTable1.*',
'JoinTable2.*',
'JoinTable3.*',
'JoinTable4.*'
)
as a parameter of your find.

Resources