eager-loading
How to enable eager loading on a association in phpactiverecord?
I have an phpactiverecord Model "Category". This Model has a self-referential has_many association, and two has_one associations to another Model. class Category extends Model { static $table_name = 'categories'; static $has_many = array( array( 'child_categories', 'class_name' => 'Category', 'primary_key' => 'categories_id', 'foreign_key' => 'parent_id' ) ); static $has_one = array( array( 'german_description', 'class_name' => 'CategoryDescription', 'primary_key' => 'categories_id', 'foreign_key' => 'categories_id', 'conditions' => 'language_id = 2' ), array( 'english_description', 'class_name' => 'CategoryDescription', 'primary_key' => 'categories_id', 'foreign_key' => 'categories_id', 'conditions' => 'language_id = 1' ) ); } I can eager-load the has_one associations when I find Category, this works without any problem: $category = ActiveRecord\Category::find( $category_id, array( 'include' => array( 'german_description', 'english_description' ) ) ); My problem is: When I iterate through all of a category's child-categories, and access the child-categories' has-one associations... foreach( $category->child_categories as $child_category ){ echo $child_category->german_description->categories_name; } ..then a new query is run every time I do so, becauce the child-category's has-one associations are not eager loaded. My question is: Is it possible to configure access a categories child-categories in a way that eager-loads the child-categories' has-one associations?
Related Links
How to enable eager loading on a association in phpactiverecord?
Rails eager loading with conditions
Rails eager loading and conditions
Eager loading for globalize2 translations