Yii v2 snippet guide II

Comparing #39 with #40

Revision #40 was created by rackycz rackycz on Oct 5, 2020, 6:31:14 PM.

Child aggregation in Parent GridView

Content

[...]
```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
[...]