From 7afecc4cdd7e664dbbd2a547c317e7140e1ea652 Mon Sep 17 00:00:00 2001 From: yuri Date: Mon, 10 Apr 2017 12:49:24 +0300 Subject: [PATCH] afterMassRelate, afterUnlinkAll hooks --- .../Espo/Core/ORM/Repositories/RDB.php | 7 +++++++ .../Espo/Modules/Crm/Services/TargetList.php | 9 +++++++++ application/Espo/ORM/Repositories/RDB.php | 20 ++++++++++++++----- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/application/Espo/Core/ORM/Repositories/RDB.php b/application/Espo/Core/ORM/Repositories/RDB.php index c99bcf3375..d95b62aa13 100644 --- a/application/Espo/Core/ORM/Repositories/RDB.php +++ b/application/Espo/Core/ORM/Repositories/RDB.php @@ -189,6 +189,13 @@ class RDB extends \Espo\ORM\Repositories\RDB implements Injectable $this->getEntityManager()->getHookManager()->process($this->entityType, 'afterRemove', $entity, $options); } + protected function afterMassRelate(Entity $entity, $relationName, array $params = array(), array $options = array()) + { + $options['params'] = $params; + + $this->getEntityManager()->getHookManager()->process($this->entityType, 'afterMassRelate', $entity, $options); + } + public function remove(Entity $entity, array $options = array()) { $result = parent::remove($entity, $options); diff --git a/application/Espo/Modules/Crm/Services/TargetList.php b/application/Espo/Modules/Crm/Services/TargetList.php index f75c5970fd..b75ad55569 100644 --- a/application/Espo/Modules/Crm/Services/TargetList.php +++ b/application/Espo/Modules/Crm/Services/TargetList.php @@ -48,6 +48,14 @@ class TargetList extends \Espo\Services\Record 'User' => 'users' ); + protected function init() + { + parent::init(); + $this->addDependencyList([ + 'hookManager' + ]); + } + public function loadAdditionalFields(Entity $entity) { parent::loadAdditionalFields($entity); @@ -178,6 +186,7 @@ class TargetList extends \Espo\Services\Record } if ($sql) { if ($pdo->query($sql)) { + $this->getInjection('hookManager')->process('TargetList', 'afterUnlinkAll', $entity, array('link' => $link)); return true; } } diff --git a/application/Espo/ORM/Repositories/RDB.php b/application/Espo/ORM/Repositories/RDB.php index acd956da5b..f832af56cb 100644 --- a/application/Espo/ORM/Repositories/RDB.php +++ b/application/Espo/ORM/Repositories/RDB.php @@ -354,22 +354,26 @@ class RDB extends \Espo\ORM\Repository protected function beforeRelate(Entity $entity, $relationName, $foreign, $data = null, array $options = array()) { - } protected function afterRelate(Entity $entity, $relationName, $foreign, $data = null, array $options = array()) { - } protected function beforeUnrelate(Entity $entity, $relationName, $foreign, array $options = array()) { - } protected function afterUnrelate(Entity $entity, $relationName, $foreign, array $options = array()) { + } + protected function beforeMassRelate(Entity $entity, $relationName, array $params = array(), array $options = array()) + { + } + + protected function afterMassRelate(Entity $entity, $relationName, array $params = array(), array $options = array()) + { } public function updateRelation(Entity $entity, $relationName, $foreign, $data) @@ -391,12 +395,18 @@ class RDB extends \Espo\ORM\Repository return null; } - public function massRelate(Entity $entity, $relationName, array $params = array()) + public function massRelate(Entity $entity, $relationName, array $params = array(), array $options = array()) { if (!$entity->id) { return; } - return $this->getMapper()->massRelate($entity, $relationName, $params); + $this->beforeMassRelate($entity, $relationName, $params, $options); + + $result = $this->getMapper()->massRelate($entity, $relationName, $params); + if ($result) { + $this->afterMassRelate($entity, $relationName, $params, $options); + } + return $result; } public function getAll()