Josh
Josh

Reputation: 942

Cakephp SUM of related field

I'm fairly new to cakephp, I need to find the SUM of a one-to-many related table inside a cakephp query.

I have table Users and table Invoices

I want a total sum of the Invoices that belong to the user when I do a find on the Users table. The result I'm looking for is to show users and aggregate totals of invoices.

I've searched and it looks like cakephp 1.3 has a feature called virtualfields, but the app I'm working on is cake 1.2... I'm not sure I want to chance an upgrade of the framework to 1.3.

Is there a way to accomplish the same thing in 1.2?

Upvotes: 0

Views: 3879

Answers (1)

Rui
Rui

Reputation: 516

Try something like this in your find (make the appropriate changes):

array('fields' => array('sum(Model.quantity) AS total')

Hope it helps.

Upvotes: 2

Related Questions