How to use join for any relationship types in CakePHP
04 Mar 2012 Add Comments Beginner LevelIn this tutorial, we talk about how to use join for multiple tables in CakePHP when association between models are not necessarily hasOne or belongsTo.
Table Of Content
Content
1. Scenario
In CakPHP(we are referring to CakePHP 1.3 here) model, by default if the association between two models is
hasOne or belongsTo, when we use find statement, CakePHP will automatically use left join sql statement to perform the database calls.
However sometimes, we want to force CakePHP to perform join for models which are connected by hasMany, and we may sometimes want to join
more than two tables together. Below is the solution.
2. Solution
When using find() method, you can pass in option using 'joins' key to force a join statement.
The find() method above will use join on five tables to get the result, that is much efficient database call, comparing to fetch results using multiple sql statements.
One important note to take here, if you are using Containable behavior on this model, you will have to set recursive=-1 before you call find() method
3. The end
Hopefully this simple tip helped you with your development.
If you like our post, please follow us on Twitter and help spread the word. We need your support to continue.
If you have questions or find our mistakes in above tutorial, do leave a comment below to let us know.



3 Comments
Glad you like it. I’m still learning CakePHP myself but some of these tutorials have helped me out a lot in understanding CakePHP.
Glad you enjoyed our tutorials.
Tweet them to let others benefit from it too.
Regards
XD
Thanks for contributing. It's helped me udnersatnd the issues.