Difference between #5 and #6 of
Displaying, sorting and filtering HasMany & ManyMany relations in CGridView

Revision #6 has been created by yJeroen on Sep 10, 2012, 6:14:06 PM with the memo:

This tutorial gives you multiple options of how to fully implement showing, sorting and filtering related data in gridviews.
« previous (#5)

Changes

Title unchanged

Displaying, sorting and filtering HasMany & ManyMany relations in CGridView

Category unchanged

Tutorials

Yii version unchanged

Tags unchanged

CGridView, sorting, filtering, has_many, many_many, gridview

Content changed

[...]
---------

### 2.1 UseCase One - Lazy Loading

**Files/Methods**
-
[models/Review::searchOne()](https://github.com/yjeroen/ManyMany/blob/master/protected/models/Review.php#L57) - [controllers/ReviewController::actionCaseOne()](https://github.com/yjeroen/ManyMany/blob/master/protected/controllers/ReviewController.php#L10) - [views/review/caseOne](https://github.com/yjeroen/ManyMany/blob/master/protected/views/review/caseOne.php#L1) - [views/review/_caseOneGrid](https://github.com/yjeroen/ManyMany/blob/master/protected/views/review/_caseOneGrid.php#L1)

**Explanation**
[...]
**Files/Methods**
-
[models/Review::searchTwo()](https://github.com/yjeroen/ManyMany/blob/master/protected/models/Review.php#L130)   - [controllers/ReviewController::actionCaseTwo()](https://github.com/yjeroen/ManyMany/blob/master/protected/controllers/ReviewController.php#L54)   - [views/review/caseTwo](https://github.com/yjeroen/ManyMany/blob/master/protected/views/review/caseTwo.php#L1)   - [views/review/_caseTwoGrid](https://github.com/yjeroen/ManyMany/blob/master/protected/views/review/_caseTwoGrid.php#L1)  

**Explanation**
[...]
You set a `$criteria->select`, that selects a GROUP_CONCAT of the data from Genre. Don't forget to set the attributes of the main Model here or else those aren't loaded. Note that you don't have to include the primary keys in this select statement. Those are automatically added by Yii.

A slight disadvantage about this method is that you can't use the loaded related data in your app like `$reviewModel->genres->name`, because the data is loaded in the class property `Review::$allGenres.
 
 
An example:

```php
[...]
**Files/Methods**
-
[extensions/classMap/CActiveFinder](https://github.com/yjeroen/ManyMany/blob/master/protected/extensions/classMap/CActiveFinder.php#L2) - [/index.php](https://github.com/yjeroen/ManyMany/blob/master/index.php#L29) - [models/Review::searchThree()](https://github.com/yjeroen/ManyMany/blob/master/protected/models/Review.php#L193) - [controllers/ReviewController::actionCaseThree()](https://github.com/yjeroen/ManyMany/blob/master/protected/controllers/ReviewController.php#L91) - [views/review/caseThree](https://github.com/yjeroen/ManyMany/blob/master/protected/views/review/caseThree.php#L1) - [views/review/_caseThreeGrid](https://github.com/yjeroen/ManyMany/blob/master/protected/views/review/_caseThreeGrid.php#L1)

**Explanation**
[...]
4. $criteria->together has been set to true

_Note: I didn't test this custom CActiveFinder as much as the KeenLoading extension.
 I don't recommend using this usecase._

### 2.4 UseCase Four - KeenLoading
[...]
**Files/Methods**
- [components/KeenActiveDataProvider](https://github.com/yjeroen/ManyMany/blob/master/protected/components/KeenActiveDataProvider.php)
-
[models/Song::search()](https://github.com/yjeroen/ManyMany/blob/master/protected/models/Song.php#L81) - [controllers/SongController::actionSongs()](https://github.com/yjeroen/ManyMany/blob/master/protected/controllers/SongController.php#L50) - [controllers/SongController::setSearchInputs()](https://github.com/yjeroen/ManyMany/blob/master/protected/controllers/SongController.php#L7) - [views/song/songsGrid](https://github.com/yjeroen/ManyMany/blob/master/protected/views/song/songsGrid.php#L1) - [views/song/_songsGrid](https://github.com/yjeroen/ManyMany/blob/master/protected/views/song/_songsGrid.php#L1)

**Explanation**
Related data is loaded in a keen fashion. Using KeenActiveDataProvider, the related models are loaded in a separate query and then put into the relation properties of the earlier loaded models.

In your Models search function, you return a new KeenActiveDataProvider, instead of a CActiveDataProvider. The KeenActiveDataProvider has another option named 'withKeenLoading', where you can set the relations that you want to load in a second(or multiple) queries.
[...]
17 0
41 followers
Viewed: 75 241 times
Version: 1.1
Category: Tutorials
Written by: yJeroen
Last updated by: yJeroen
Created on: Sep 9, 2012
Last updated: 12 years ago
Update Article

Revisions

View all history