在Yii中即时排除相关的模型数据

时间:2012-10-22 17:44:53

标签: php yii

是否可以在运行中从查找条件中排除相关的模型数据?

我有一个Post模型,HAS_MANY Images和我做一个findByPK($ id),它同时加载了与Post相关的Post和Images模型。

如何动态排除这些相关模型?

1 个答案:

答案 0 :(得分:1)

它实际上并没有加载任何东西,Yii默认使用延迟加载。也就是说它只根据要求加载相关的模型。这样:

$post = Post::model()->findByPK($id);
//At this point images are not yet loaded

When you call the images, Yii notices they are not loaded and loads them
$post->images;

这意味着,除非您需要,否则不会加载图像。