Given the following models:
Category: has_many(‘template’) Template: has_many(‘tag’, ‘procedure’)
What is the most efficient way to load ALL objects related to ALL categories?
For instance, I’m currently doing the following, but hopefully there is a better way:
// Load all Category objects
$categories = new Category();
$categories->get();
// Load all Template objects related to each Category
foreach($categories as $category)
{
$category->templates->get();
// Load all the Tag and Procedure objects related to each template
foreach($category->templates as $template)
{
$template->tags->get();
$template->procedures->get();
}
}
Right now this code is executing over 200 queries on one particular page.
You can use the following
Make sure you have activated the autopopulate option in the datamapper config application/config/datamapper.php and you dont need to run get() on all models
Hope it helps!