I have three models associated with each other in my CakePHP application. I have an Article
which belongs to both a Category
and an Author
. I query for an category’s latest articles like this:
$category = $this->Category->find('first', array(
'conditions' => array(
'Category.slug' => $slug
),
'contain' => array(
'Article' => array(
'Author'
)
)
));
However, I’ve noticed this is very inefficient, as for every Article
record it finds it queries my authors
database table, even if I’ve already fetched that particular author’s details. So if I fetch 10 articles and they’re all by the same author, CakePHP will still issue 10 separate SELECT
statements.
Can I make CakePHP “smarter” when using the Containable behavior so that it only issues the necessary queries? In my case, issue the first SELECT
statement to find the author, and then use that result in subsequent Article
rows if the ID matches.