Reputation: 317
Lets assume following situation:
Total quantities in B sums all individual quantities of all relevant individual records in A.
I have ModelC/edit where the user can add/delete/modify A.
I want to update B.total_quantity each time when records in A were appended, modified or deleted.
Basically, I have 3 possibilities to do the update:
My first pick was solution 2, but I have found problems about it, so I need help.
In the ModelA.AfterSave I have only new, modified data. How could I know if A.quantity has been changed? There is no $this->old['ModelA'] or something like that. Even if I could know that information, I don't have access to ModelB, because there is no $this->data['ModelB']...
Anybody can help with this?
Upvotes: 0
Views: 269
Reputation: 3701
You could study AggregateCache Behavior for CakePHP 2.x, here is a little snippet that should sit in your model B :
public $actsAs = array(
'AggregateCache'=>array(
array('field'=>'quantity','model'=>'A', 'sum'=>'total_quantity','recursive'=>-1),
)
);
UPDATE : Full code now available @Github: https://github.com/cwbit/cakephp-aggregate-cache
Vincent Lizzi put up the code here : http://bakery.cakephp.org/articles/vincentm8/2010/08/23/aggregatecache-behavior
Upvotes: 1