如何在phpactiverecord中启用对关联的预先加载?

时间:2014-04-01 10:15:29

标签: eager-loading phpactiverecord

我有一个phpactiverecord模型"类别"。该模型具有自引用的has_many关联,以及与另一个模型的两个has_one关联。

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'
        )
    );

}

当我找到Category时,我可以急切加载has_one关联,这没有任何问题:

$category = ActiveRecord\Category::find(
    $category_id,
    array(
        'include' => array( 'german_description', 'english_description' )
    )
);

我的问题是:当我遍历所有类别的子类别时,访问子类别'有一个协会......

foreach( $category->child_categories as $child_category ){
    echo $child_category->german_description->categories_name;
}

..然后每次我都会运行一个新的查询,因为子类别的一个关联不是急切加载的。

我的问题是:是否可以按照急切加载子类别的方式配置类别子类别的访问权限。有一个协会?

0 个答案:

没有答案