Another way is to write a custom code using afterSave() entity, which would automatically sum all values from ROZPOTYs and updated ID CISLO UNIE - after saving your ROZPOTYs record.
You can see here example code for recalculating Opportunity 'Amount' field everytime I add new or remove OpportunityProduct (custom entity) to the Opportunity:
Info: The method recalculateOpportunityPrice() is being called from afterSave() method on OpportunityProduct
PHP Code:
/**
* @param Entity $opportunity Opportunity that its Amount should be recalculated.
* @returns void Since it only recalculates Amount on Opportunity, nothing is returned.
*/
private function recalculateOpportunityPrice(Entity $opportunity): void
{
$entityManager = $this->entityManager;
$opportunityProducts = $entityManager
->getRepository($this->entityType)
->getRelation($opportunity, 'opportunityProducts')
->find();
$totalAmount = 0.00;
foreach ($opportunityProducts as $opportunityProduct)
{
$price = $opportunityProduct->get('totalPrice');
$totalAmount += $price;
}
$opportunity->set('amount', $totalAmount);
$entityManager->saveEntity($opportunity);
}
Leave a comment: