Load Models Without Its Relations (Has_Many Or Many_Many) ...!


How i can load models without its relations (HAS_MANY or MANY_MANY) …?!

Thats so heavy some times ! ;)

Just don’t specify with.

I don’t.

I just use

$p = Post::model()->find(...)


$p = $this->loadModel($id, 'Post');

my post model has a many_many relation with tags model.



is allocated and i want prevent from this allocation.


will not be allocated until you access it. When you do a normal find() you are only getting the records attributes. Relations are then populated on demand when you access them. They will not be loaded when you do not need them.

My Problem was not solve YET!

I have a gridview of X model and it’s column is: a, b, c.

X model has a relation with Y model named XY in X model declaration.

like this:

public function relations() {

	return array(

		'XY' => array(self::HAS_MANY, 'Y', 'idY'),		



In this gridview 150 model was loaded (in 15 page).

I was log all queries that executed in this page by a components (db_profiler).

I see more than 150 queries that was executed for loading Y model of each X model.

[color="#FF0000"]And this is terrible for hug databases !!!


You can use CDbCriteria::with and together option to load records in the initial query.

What i should set for CDbCriteria::with? null? false? …? :huh: