Difference between #58 and #59 of
Filter & Sort by calculated/related fields in GridView Yii 2.0

Revision #59 has been created by Kabinenkoffer on Jun 13, 2018, 7:56:53 AM with the memo:

Code not highlighted correct
« previous (#58) next (#60) »

Changes

Title unchanged

Filter & Sort by calculated/related fields in GridView Yii 2.0

Category unchanged

Tutorials

Yii version changed

2.0

Tags changed

wiki, faq, grid, gridview, yii2, dfilter,gridview,faq,sort,DatacColumn, calculated, filter, sortyii2,wiki,grid,calculated

Content changed

[...]
-----------------
Let's say you have the following tables:
~~~
[sql]
/* Countries */
```MYSQL
 
CREATE TABLE `tbl_country` ( `id` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'Unique country identifier', `country_name` VARCHAR(150) NOT NULL COMMENT 'Country name', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Country master table'; ```
 
 
/* Persons */ ```MYSQL
 
CREATE TABLE `tbl_person` (
`id` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'Unique person identifier',
`first_name` VARCHAR(60) NOT NULL COMMENT 'First name',
[...]
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Person master table';
```
 
 
/* Foreign Key */ ```MYSQL
 
ALTER TABLE `tbl_person` ADD CONSTRAINT `tbl_person_FK1` FOREIGN KEY (`country_id`) REFERENCES `tbl_country` (`id`) , ADD INDEX `tbl_person_FK1` (`country_id` ASC); ```
 
 
~~~

Prerequisites
[...]
**STEP 1:** Add a getter function to your base _Person_ model:
### Setup base model

```php

/* Getter for person full name */
public function getFullName() {
[...]
**STEP 2:** Add an attribute _fullName_ to your model _PersonSearch_ and configure your rules.
### Setup search model

```php

/* your calculated attribute */
public $fullName;
[...]
**STEP 3:** Configure your gridview columns in your view _index_ file
### Setup view file

```php

echo GridView::widget([
'dataProvider' => $dataProvider,
[...]
**STEP 1:** Ensure your _Person_ model has a relation defined to the _Country_ model. You can also implement a getter for CountryName.
### Setup base model

```php

/* ActiveRelation */
public function getCountry()
[...]
**STEP 2:** Add an attribute _countryName_ to your model _PersonSearch_ and configure your rules.
### Setup search model

```php

/* your calculated attribute */
public $countryName;
[...]
**STEP 3:** Configure your gridview columns in your view _index_ file
### Setup view file

```php

echo GridView::widget([
'dataProvider' => $dataProvider,
[...]
**STEP 1:** Ensure your _Person_ model has a self join relation defined to itself. You can also implement a getter for ParentName.
### Setup base model

```php

/* ActiveRelation */
public function getParent() {
[...]
**STEP 2:** Add an attribute _parentName_ to your model _PersonSearch_ and configure your rules.
### Setup search model attributes for search

```php

/* your calculated attribute */
public $parentName;
[...]
**STEP 3:** Edit your _addCondition_ function in the model _PersonSearch_
### Setup search model condition

```php

protected function addCondition($query, $attribute, $partialMatch = false)
{
[...]
**STEP 4:** Configure your gridview columns in your view _index_ file
### Setup view file

```php

echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[...]
13 3
64 followers
Viewed: 250 192 times
Version: 2.0
Category: Tutorials
Written by: Kartik V
Last updated by: softark
Created on: Feb 9, 2014
Last updated: 5 years ago
Update Article

Revisions

View all history