diff --git a/application/Espo/Modules/Crm/Repositories/Account.php b/application/Espo/Modules/Crm/Repositories/Account.php index 127fa1af6a..6e7e465964 100644 --- a/application/Espo/Modules/Crm/Repositories/Account.php +++ b/application/Espo/Modules/Crm/Repositories/Account.php @@ -41,5 +41,25 @@ class Account extends \Espo\Core\ORM\Repositories\RDB $this->relate($entity, 'targetLists', $entity->get('targetListId')); } } + + protected function afterRelateContacts(Entity $entity, $foreign, $data) + { + if (!($foreign instanceof Entity)) return; + + if (!$foreign->get('accountId')) { + $foreign->set('accountId', $entity->id); + $this->getEntityManager()->saveEntity($foreign); + } + } + + protected function afterUnrelateContacts(Entity $entity, $foreign, $data) + { + if (!($foreign instanceof Entity)) return; + + if ($foreign->get('accountId') && $foreign->get('accountId') === $entity->id) { + $foreign->set('accountId', null); + $this->getEntityManager()->saveEntity($foreign); + } + } }