Love Fuel?
Donate
About
Forums
Discussions
Login
FuelPHP Forums
Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Orm
Best practice for finding sum-values of related models-array
bernhardh
June 2013
Hello,
I have the following situation:
class Model_Customer extends \Orm\Model
{
protected static $_has_many = array(
'invoices' => array( 'model_to' => 'Model_Invoice', )
);
}
class Model_Invoice extends \Orm\Model
{
protected static $_properties = array(
....
'value'
);
}
Now I want to get the sum of the invoice-values of one customer.
The following code would work, but it is the best way (performance)?
$sum = 0;
foreach( $this->invoices AS $invoice ) {
$sum += $invoice->getNetto();
}
Harro
June 2013
Accepted Answer
It depends, you'll have to test it.
You should make sure "invoice" is included in the query (using related), to avoid a second query due to lazy loading. And compare the additional impact of the join (and the loop) with a separate SUM query.
Add a Comment
Howdy, Stranger!
It looks like you're new here. If you want to get involved, click one of these buttons!
Sign In
Apply for Membership
Categories
All Discussions
5,088
General
↳ General
3,364
↳ Job Board
13
↳ Installation & Setup
214
Packages
↳ Oil
213
↳ Orm
700
↳ Auth
260
Development
↳ Tips and Tutorials
126
↳ Code share
145
↳ Applications
52
In this Discussion
Harro
June 2013