1

I'm new to Yii. Currently I'm learning about Active Record lazy loading and eager loading. I have searched internet but couldn't find any good article which explain in what condition should I use those loading method. Any one here could explain it?

Thanks.

rahmat
  • 1,727
  • 16
  • 35
  • It all depends on what you are looking to do. Lazy loading is defined as "defer initialization of an object until the point at which it is needed" So if you are doing something like infinite scrolling, you would only be creating an Active Record object with the data set as the user scrolls to a certain point on the page. There are class methods in CActiveDataProvider which will help you determine where in a data set you are (for example, to find the pagination) – Brett Gregson Oct 14 '12 at 13:57

1 Answers1

0

This is all about the n+1 problem. From the Yii guide

By default, a single JOIN statement will be generated and executed for all relations involved in the eager loading. If the primary table has its LIMIT or OFFSET query option set, it will be queried alone first, followed by another SQL statement that brings back all its related objects. Previously in version 1.0.x, the default behavior is that there will be N+1 SQL statements if an eager loading involves N HAS_MANY or MANY_MANY relations.

Community
  • 1
  • 1
Asgaroth
  • 4,274
  • 3
  • 20
  • 36