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.[...]
The following are the examples of the effort to answer it.
- [wiki - Drills : Search by a HAS_MANY relation in Yii 1.1](http://www.yiiframework.com/wiki/428/
drills-search-by-a-has_many-relation "Drills : Search by a HAS_MANY relation
in Yii 1.1")
- [wiki - Displaying, sorting and filtering HasMany & ManyMany relations in CGridView](http://www.yiiframework.com/wiki/385/displaying-sorting-and-filtering-hasmany-manymany-relations-in-cgridview "Displaying, sorting and filtering HasMany & ManyMany relations in CGridView")
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.
Degraded? Never! It's cleaner and more effective than in Yii 1.1.
- [wiki - Relational Query - Lazy Loading and Eager Loading in Yii 2.0](http://www.yiiframework.com/wiki/834/ "Relational Query - Lazy Loading and Eager Loading in Yii 2.0")