Difference between #45 and #46 of
Yii v2 snippet guide II

Revision #46 has been created by rackycz on Oct 5, 2020, 8:13:16 PM with the memo:

Search sort related column
« previous (#45) next (#47) »

Changes

Title unchanged

Yii v2 snippet guide II

Category unchanged

Tutorials

Yii version unchanged

2.0

Tags unchanged

tutorial,beginner,yii2,snippets

Content changed

[...]
[['items'], 'integer'],
];
}
```

Into method search() add
HAVING condition if you want to filter by amount or items: ```php if (trim($this->amount)!=='') { $query->andHavingFilterWhere([
'amount' => $this->amount
]);
[...]
// ...

    // You must use joinWith() in order to have both tables in one JOIN - then you can call WHERE and ORDER BY on the 2nd table. 
 
    // Explanation here:
 
    // https://stackoverflow.com/questions/25600048/what-is-the-difference-between-with-and-joinwith-in-yii2-and-when-to-use-them
 
    
 
    $query = Invoice::find()->joinWith('company');
 
 
// Appending new sortable column:
$sort = $dataProvider->getSort();
$sort->attributes['company_name'] = [
[...]
if (trim($this->company_name)!=='') {
$query->and
HavingFilterWhere(['like', 'table.column', $this->company_name]); } ```
4 0
3 followers
Viewed: 75 359 times
Version: 2.0
Category: Tutorials
Written by: rackycz
Last updated by: rackycz
Created on: Aug 26, 2020
Last updated: 3 years ago
Update Article

Revisions

View all history