I have an Invoices Table and a LineItems Table. I want to be able to return the invoice total when I get a list of the invoices on the index action. So I would like to return the Invoice, LineItems and the total of LineItems.price field as a Total.
Current index code in the controller is the default generated.
public function index()
{
$this->paginate = [
'contain' => ['Clients', 'Users', 'LineItems']
];
$this->set('invoices', $this->paginate($this->Invoices));
$this->set('_serialize', ['invoices']);
}
Should I create a map and reduce for this associated item to get the total or is there a better way maybe extending the select to include an additional field. I figured out how to add the select but not how to add it as an additional field. If I overwrite LineItems I just end up with the total and not the other data.
Any pointers on how to do a map reduce in this instance on an associated data or is there a better way?