Yii Framework Forum: Load Models Without Its Relations (Has_Many Or Many_Many) ...! - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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

#1 User is offline   MSN_Issue 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 07-April 13

Posted 05 May 2013 - 12:57 AM

Hi
How i can load models without its relations (HAS_MANY or MANY_MANY) ...?!
Thats so heavy some times ! ;)
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,016
  • Joined: 17-January 09
  • Location:Russia

Posted 05 May 2013 - 03:22 AM

Just don't specify with.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#3 User is offline   MSN_Issue 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 07-April 13

Posted 05 May 2013 - 04:01 AM

I don't.
I just use
$p = Post::model()->find(...)

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



my post model has a many_many relation with tags model.
so
$p->tags
is allocated and i want prevent from this allocation.
0

#4 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 538
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 05 May 2013 - 07:59 AM

View PostMSN_Issue, on 05 May 2013 - 04:01 AM, said:

so
$p->tags
is allocated and i want prevent from this allocation.


$p->tags
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.
0

#5 User is offline   MSN_Issue 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 07-April 13

Posted 17 July 2013 - 03:12 AM

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.

And this is terrible for hug databases !!!

0

#6 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 538
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 17 July 2013 - 03:45 AM

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

#7 User is offline   MSN_Issue 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 07-April 13

Posted 17 July 2013 - 04:21 AM

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users