Difference between #7 and #12 of
Relational Query - Lazy Loading and Eager Loading in Yii 1.1 / with and together

Revision #12 has been created by softark on Jan 9, 2016, 6:27:21 AM with the memo:

added link to 2.0 version of this article
« previous (#7) next (#13) »

Changes

Title changed

Relational Query - Lazy Loading and Eager Loading in Yii 1.1 / with and together

Category unchanged

Tutorials

Yii version unchanged

Tags unchanged

relational query, eager loading, lazy loading, with, together

Content changed

It's well known that there are **Lazy Loading** approach and **Eager Loading** approach in the relational query. But it's important to note that you should distinguish 2 different modes in the Eager Loading in Yii 1.1.x. 1. Lazy Loading 2. Eager Loading 1. Single Query 2. DoubMultiple Query

So there are virtually 3 approaches.
[...]
- An Author HAS_MANY Posts


 
```php /* Author.php */
 
public function relations() { // NOTE: you may need to adjust the relation name and the related
 
// class name for the relations automatically generated below.
 
    return array(         'posts' => array(self::HAS_MANY, 'Post', 'author_id'),     ); } ``` - A Post BELONGS_TO an Author
 
```php /* Post.php */
 
public function relations() {
 
        // NOTE: you may need to adjust the relation name and the related
 
        // class name for the relations automatically generated below.
 
        

 
{
 
    
return array(                     'author' => array(self::BELONGS_TO, 'UseAuthor', 'author_id'),         );
 
    
    );
 
} ```
 
Lazy Loading
------------
[...]
Yii will loop through the result set to populate the array of Authors and their Posts in a single shot.

Eager Loading (
DoubMultiple Query) ---------------------------- The following is an example of eager loading in doubmultiple query mode.
[...]
Looping through the 2 result sets, Yii will populate the array of Authors with their Posts.

Note that the total number of queries is 2 because we include only one HAS_MANY relation here. It can be 3 or more when you have included 2 or more HAS_MANY or MANY_MANY relations.
 
 
Which Loading Approach?
-----------------------
OK. Then, which loading approach will Yii use for a relational query?
[...]
- BELONGS_TO or HAS_ONE => **Single Query**
- HAS_MANY or MANY_MANY
- with **LIMIT** and/or **OFFSET** => **
DoubMultiple Query** - without **LIMIT** or **OFFSET** => **Single Query** - **together** is false => **DoubMultiple Query**
- **together** is true => **Single Query**
[...]
- If you want to filter the result by an attribute in the related model, you have to use the single query eager loading, otherwise you will get `column not found` error.
- But if you want to limit the number of main model correctly, you have to use the
doubmultiple query eager loading (or lazy loading).

What if you want both of them at the same time? There seems to be no easy answer to it.
[...]
Notice
------
"Single Query" and "
DoubMultiple Query" are the terms that I molded myself. They are not from the official documentation. But bearing them in mind, you will find things get easier to comprehend. And one more thing ... this article is Yii 1.1.x specific. Yii 2 doesn't have Single Query Eager Loading.
 
 
- [wiki - Relational Query - Eager Loading in Yii 2.0](http://www.yiiframework.com/wiki/834/relational-query-eager-loading-in-yii-2-0/ "Relational Query - Eager Loading in Yii 2.0")
 
9 0
22 followers
Viewed: 60 634 times
Version: 1.1
Category: Tutorials
Written by: softark
Last updated by: softark
Created on: Jul 6, 2013
Last updated: 8 years ago
Update Article

Revisions

View all history