How do I find the queries being generated?
CodeIgniter’s profiler: from your controller run
$this->output->enable_profiler(TRUE);
and check the queries at the bottom of the page.
There is more than one per widget - there isn’t any way to do without calling get() in the foreach? Does that increase the number of queries or simply return an existing object?
Calling get() in foreach would increase the number of queries. That’s why Phil first recommended using include_related. As long as you have either one-to-many or one-to-one relationships, you can use include_related. The way your models are set up, you could do something like
$versions = new Widget_version();
$versions->include_related('widget')->get();
foreach ($versions as ...
and get all your versions and widgets in one result set. Now, of course, to display widget-by-widget, you’d need extra logic. And ratings wouldn’t lend themselves to include_related, as you’ve defined a many-to-many relationship. Also with ratings, I think you might want to take a look at “workflow_version” in your relationship, for starters.
To elaborate a little on what Phil said earlier about not loading a Widget_version for each Widget,
$widgets->get()->widget_version->get();
does not do what you seem to think (judging by your later code and comment). The confusion likely has to do with the dual nature of DMZ objects as database records and iterators. So, $widgets->get() loads all records from your widgets table and stores them such that they can be iterated over, however, when you access the widget_version field on the object, it applies to the object-as-a-record (not to all returned records). A Widget_version object is instantiated with its parent set to the first record stored in $widgets. Then when get() is called on widget_version, only Widget_version records related to that first Widget are returned. Later, when you’re iterating over all widgets, you’re dealing with a set of individual objects which don’t refer to the original object’s related Widget_versions you pulled from the database. That’s why you get all the widgets fine, but see no version information at all. That’s a little tricky, so I hope that my explanation makes sense.
You might need some more time with the manual and and picking apart the example application. Or, depending on how you learn best, turning on the profiler so you can see if the generated queries are doing what you think they should might be your best option.