I have a query along the lines of this:
SELECT * FROM posts WHERE owner_id IN (SELECT owner_id FROM owners_list WHERE user_id = 1) ORDER BY modified DESC
What is the most efficient Cake find call that is equivalent? Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
3 choices:
1) Cake does handle sub-queries, though it is complex:
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#sub-queries
Here’s a SO question that has an example:
Subquery in cakePHP to lookup in other table
2) Use a join statement:
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#joining-tables
3) You can just use your query as-is via via Model::query() as long as you sanitize first
I prefer option 2 as it’s less prone to errors and I don’t have to worry about sanitizing/escaping. And once you get the hang of joins you’ll find yourself using them more often.