From 187e0076534a0802c50639498877df060d1a8a7b Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Tue, 7 Apr 2020 16:25:43 +0300 Subject: [PATCH] formula record relate support mass relate --- .../Functions/RecordGroup/RelateType.php | 14 ++++++++++++-- .../Espo/Core/Formula/FormulaTest.php | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/application/Espo/Core/Formula/Functions/RecordGroup/RelateType.php b/application/Espo/Core/Formula/Functions/RecordGroup/RelateType.php index 76a8cc644a..52cc1b92ee 100644 --- a/application/Espo/Core/Formula/Functions/RecordGroup/RelateType.php +++ b/application/Espo/Core/Formula/Functions/RecordGroup/RelateType.php @@ -61,9 +61,19 @@ class RelateType extends \Espo\Core\Formula\Functions\Base $entity = $em->getEntity($entityType, $id); if (!$entity) return null; - if ($em->getRepository($entityType)->isRelated($entity, $link, $foreignId)) + if (is_array($foreignId)) { + foreach ($foreignId as $itemId) { + $em->getRepository($entityType)->relate($entity, $link, $itemId); + } return true; - return $em->getRepository($entityType)->relate($entity, $link, $foreignId); + } else if (is_string($foreignId)) { + if ($em->getRepository($entityType)->isRelated($entity, $link, $foreignId)) { + return true; + } + return $em->getRepository($entityType)->relate($entity, $link, $foreignId); + } else { + throw new Error("Formula record\\relate: foreignId type is wrong."); + } } } diff --git a/tests/integration/Espo/Core/Formula/FormulaTest.php b/tests/integration/Espo/Core/Formula/FormulaTest.php index 8946e800dc..4934be7a91 100644 --- a/tests/integration/Espo/Core/Formula/FormulaTest.php +++ b/tests/integration/Espo/Core/Formula/FormulaTest.php @@ -421,6 +421,25 @@ class FormulaTest extends \tests\integration\Core\BaseTestCase $this->assertTrue($em->getRepository('Account')->isRelated($a, 'opportunities', $o)); } + public function testRecordRelate1() + { + $fm = $this->getContainer()->get('formulaManager'); + $em = $this->getContainer()->get('entityManager'); + + $a = $em->createEntity('Account', [ + 'name' => '1', + ]); + $o = $em->createEntity('Opportunity', [ + 'name' => '1', + ]); + + $script = "record\\relate('Account', '".$a->id."', 'opportunities', list('".$o->id."'))"; + $result = $fm->run($script, $contact); + + $this->assertTrue($result); + $this->assertTrue($em->getRepository('Account')->isRelated($a, 'opportunities', $o)); + } + public function testRecordUnrelate() { $fm = $this->getContainer()->get('formulaManager');