0

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.

Martin Bean
  • 38,379
  • 25
  • 128
  • 201

1 Answers1

0

There are several ways of doing this through cake's api - the simplest would be to either set the Model's $recursive attribute to either -1 or 0, or use the Model's unbind() function

Jon Ashdown
  • 176
  • 4
  • 11