I have a bunch of invoice records displayed in gridview. Each invoice has a HAS_MANY relation to line items. Each line item has an amount among other fields. What I would like to do is display the total* of those line items in the invoice gridview.
Grid Example
ID | Invoice Title | Invoice Status | Invoice Date | Invoice Total*
I’ve done a bit of searching through the forums but can’t quite find what I’m looking for.
I have invoice and invoice_line tables. On the invoice.php model, I add one variable $invoiceTotal in addition to afterFind method which will read the values on its invoice_line and set the $invoiceTotal value. It should be working but I just do not know whether this is the correct yii way and also not quite sure with the performance impact.
Sorting and searching, I never check this since usually I am not doing any sorting and searching on the calculated columns.
See my solution of customizing grid datacells: Merge GridView Cells
It should be easy to generate a controller method, that returns your total invoice sum.
class GridController extends Controller
{
protected function getInvoiceTotal($data,$row)
{
$invoiceTotal = 0;
....
$invoiceId = $data->ID;
//do db-queries here ...
....
return $invoiceTotal;
}
$this->widget('zii.widgets.grid.CGridView', array(
....
'columns' => array(
....
array(
'name' => 'InvoiceTotal',
//call the method 'getInvoiceTotal' of the controller
//the params extracted to the method are $data (=the current rowdata) and $row (the row index)
'value' => array($this, 'getInvoiceTotal')
),
I already have a relation in the Invoice model to ‘br5_InvItems’ is the above code making an unnecessary call to the db. Can the existing relation be used to tidy the code?
Also the query returns an array, I’ve just grabbed the first value to return a string, looks kinda messy - any suggestions?
…Thanks Joblo for pointing me in the right direction.