I have two entities: A and B. Entity A has many Bs. I want to update a value in A after unlinking one B from the collection of Bs. Which hook should I use to accomplish this? Thank you
Announcement
Collapse
No announcement yet.
Which Hook to Use for Updating Entity A After Unlinking a B?
Collapse
X
-
Hello alohatech
Here you have a list of hooks: Hooks - EspoCRM Documentation
Based on the description afterUnrelate seems the best.
-
Like this?PHP Code:<?php
namespace Espo\Custom\Hooks\A;
use Espo\ORM\Entity;
use Espo\Core\Hooks\Base;
class AfterUnrelate extends \Espo\Core\Hooks\Base
{
public static $order = 10;
public function afterUnrelate(Entity $entity, array $options)
{
// Fetch all B records related to this A
$BList = $entity->get('Bs');
$total = 0; // Initialize as 0
// Sum the price values from all related Bs records
foreach ($BList as $B) {
$total += (float) $B->get('price');
}
// Update the total field in the A entity
$entity->set('total', $total);
}
}
- Likes 1
Comment
-
- Likes 1
Comment
Comment