Exclude Related Model In Find Query

Hi there, I have following relations


Category HAS_MANY Post

Post HAS_ONE Category

Post HAS_ONE Location

Post HAS_MANY Image

In my actionView in CategoryController, I would like to load the Category model data and at the same time load the Post model data excluding the Location and Image model.

I noticed that even when I do a find; like the one below:


$model=Category::model()->findByPk($id);

It automatically loads the Location and Image model data that is related the Post as well.

How do I exclude them out on the fly?

Thank you.

Are you sure it’s loading them at that point? Yii uses lazy loading in this sort of situation and only loads the data from the database when you first access the relation.

To get Category and Post records at the same time, you’d use:




$model = Category::model()->with('Post')->findByPk($id);



The Location and Image records won’t be loaded until you attempt to access them through their relations.