Yii Framework Forum: Lazy & Eager Loading - Yii Framework Forum

Jump to content

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

Lazy & Eager Loading Rate Topic: -----

#1 User is offline   Masoud Sawari 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 103
  • Joined: 24-January 14
  • Location:Iran, Khuzestan, Ahwaz

Posted 22 April 2014 - 03:54 AM

The following text is taken from this article. I don't know which ones lazy loading and which are eager loading:

Quote

For example, in order to use separate SQL statements to query for the latest posts with their comments, we can declare the comments relation in Post class as follows,

public function relations()
{
    return array(
        'comments' => array(self::HAS_MANY, 'Comment', 'post_id',
                        'together'=>false),
    );
}


We can also dynamically set this option when we perform the eager loading:

$posts = Post::model()->with(
          array('comments'=>array(
             'together'=>false
             ))
          )->findAll();


sorry for my bad English!
0

#2 User is offline   rob006 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 90
  • Joined: 01-January 13
  • Location:Lublin, Poland

Posted 22 April 2014 - 04:05 AM

Both examples are lazy loading.
0

#3 User is offline   Masoud Sawari 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 103
  • Joined: 24-January 14
  • Location:Iran, Khuzestan, Ahwaz

Posted 22 April 2014 - 06:48 AM

View Postrob006, on 22 April 2014 - 04:05 AM, said:

Both examples are lazy loading.


Hi, but in the second example it said that eager!
sorry for my bad English!
0

#4 User is offline   jkofsky 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 536
  • Joined: 17-May 10
  • Location:Pensacola, Florida

Posted 22 April 2014 - 09:47 PM

View PostMasoud Sawari, on 22 April 2014 - 03:54 AM, said:

The following text is taken from this article. I don't know which ones lazy loading and which are eager loading:


My understanding is:

Given
public function relations()
{
    return array(
        'comments' => array(self::HAS_MANY, 'Comment', 'post_id',
                        'together'=>false),
    );
}


Lazy loading:
Controller
   $model = Post::model()->findAll();

View:
   echo $model->comments->title;


Eager Loading:
Controller
   $model = Post::model()->with('comments')->findAll();

View:
   echo $model->comments->title;


Eager loading generates the SQL to pull the related information in the initial findByXXX() call. Lazy loading makes another SQL call when you first reference the relation's information.
Do, or do not. There is no 'try.' Jedi Master Yoda
0

#5 User is offline   Masoud Sawari 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 103
  • Joined: 24-January 14
  • Location:Iran, Khuzestan, Ahwaz

Posted 23 April 2014 - 12:00 AM

View Postjkofsky, on 22 April 2014 - 09:47 PM, said:

My understanding is:

Given
public function relations()
{
    return array(
        'comments' => array(self::HAS_MANY, 'Comment', 'post_id',
                        'together'=>false),
    );
}


Lazy loading:
Controller
   $model = Post::model()->findAll();

View:
   echo $model->comments->title;


Eager Loading:
Controller
   $model = Post::model()->with('comments')->findAll();

View:
   echo $model->comments->title;


Eager loading generates the SQL to pull the related information in the initial findByXXX() call. Lazy loading makes another SQL call when you first reference the relation's information.


Thank you, but I still don't understand what is the benefit of CDbCriteria::together property :(
sorry for my bad English!
0

#6 User is offline   jkofsky 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 536
  • Joined: 17-May 10
  • Location:Pensacola, Florida

Posted 23 April 2014 - 12:02 AM

View PostMasoud Sawari, on 23 April 2014 - 12:00 AM, said:

Thank you, but I still don't understand what is the benefit of CDbCriteria::together property :(

I had to use 'together'=>true; to get the CDbCriteria to actually pull in a relation.
Do, or do not. There is no 'try.' Jedi Master Yoda
0

#7 User is offline   Masoud Sawari 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 103
  • Joined: 24-January 14
  • Location:Iran, Khuzestan, Ahwaz

Posted 23 April 2014 - 02:54 AM

View Postjkofsky, on 23 April 2014 - 12:02 AM, said:

I had to use 'together'=>true; to get the CDbCriteria to actually pull in a relation.


When we don't use with() method, always lazy loading performed whether or not use together property. When we use with() method, if together set true, eager loading performed. if together set false, lazy loading performed. if together not set, considered to be true.
sorry for my bad English!
0

#8 User is offline   georaldc 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 170
  • Joined: 08-July 11

Posted 23 April 2014 - 04:01 PM

View PostMasoud Sawari, on 22 April 2014 - 06:48 AM, said:

Hi, but in the second example it said that eager!

The wording might have confused you. It says, "We can also dynamically set this option when we perform the eager loading", the key being "when we perform the eager loading". This is eager loading:

$posts = Post::model()->with(
            array('comments')
        )->findAll();


Adding the together option and setting it to false in an eager loading setup like this separates the SQL statements, turning it into a lazy load approach. That's what the article was trying to explain.
1

#9 User is offline   Masoud Sawari 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 103
  • Joined: 24-January 14
  • Location:Iran, Khuzestan, Ahwaz

Posted 24 April 2014 - 02:28 AM

View Postgeoraldc, on 23 April 2014 - 04:01 PM, said:

The wording might have confused you. It says, "We can also dynamically set this option when we perform the eager loading", the key being "when we perform the eager loading". This is eager loading:

$posts = Post::model()->with(
            array('comments')
        )->findAll();


Adding the together option and setting it to false in an eager loading setup like this separates the SQL statements, turning it into a lazy load approach. That's what the article was trying to explain.


Thank you very much georaldc. I don't know English well. :lol:
sorry for my bad English!
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