Difference between #39 and #40 of
Yii v2 snippet guide II

Revision #40 has been created by rackycz on Oct 5, 2020, 6:31:14 PM with the memo:

Child aggregation in Parent GridView
« previous (#39) next (#41) »

Changes

Title unchanged

Yii v2 snippet guide II

Category unchanged

Tutorials

Yii version unchanged

2.0

Tags unchanged

tutorial,beginner,yii2,snippets

Content changed

[...]
```sql
CREATE VIEW v_invoice AS
SELECT invoice.*,
SUM(invoice_item.units * invoice_item.price_per_unit) as amount,
COUNT(*count(invoice_item.id) as items
FROM invoice
LEFT JOIN invoice_item
[...]
```

**Note:** Here you can read why it is better not to use COUNT(*):
 
- [https://learnsql.com/blog/introduction-using-aggregate-functions-joins/](https://learnsql.com/blog/introduction-using-aggregate-functions-joins/)
 
- Chapter: Dealing with NULLs
 
 
This will technically clone the original table "invoice" into "v_invoice" and will append 2 calculated columns: "amount" + "items". Now you can easily use this VIEW as a TABLE (for reading only) and display it in a GridView. If you already have a GridView for table "invoice" the change is just tiny. Create this model:

```php
<?php
namespace app\models;
class v_Invoice extends Invoice
[...]
4 0
3 followers
Viewed: 75 361 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