diff --git a/application/Espo/Core/Formula/Functions/RecordGroup/RelateType.php b/application/Espo/Core/Formula/Functions/RecordGroup/RelateType.php index c6e495408d..00bd28aebb 100644 --- a/application/Espo/Core/Formula/Functions/RecordGroup/RelateType.php +++ b/application/Espo/Core/Formula/Functions/RecordGroup/RelateType.php @@ -31,8 +31,8 @@ namespace Espo\Core\Formula\Functions\RecordGroup; use Espo\Core\Formula\ArgumentList; use Espo\Core\Formula\Functions\BaseFunction; - use Espo\Core\Di; +use stdClass; class RelateType extends BaseFunction implements Di\EntityManagerAware @@ -49,6 +49,7 @@ class RelateType extends BaseFunction implements $id = $this->evaluate($args[1]); $link = $this->evaluate($args[2]); $foreignId = $this->evaluate($args[3]); + $columnData = count($args) > 4 ? $this->evaluate($args[4]) : null; if (!$entityType || !is_string($entityType)) { $this->throwBadArgumentType(1, 'string'); @@ -62,6 +63,14 @@ class RelateType extends BaseFunction implements $this->throwBadArgumentType(3, 'string'); } + if ($columnData !== null && !$columnData instanceof stdClass) { + $this->throwBadArgumentType(4, 'object'); + } + + if ($columnData instanceof stdClass) { + $columnData = get_object_vars($columnData); + } + if (!$foreignId) { return null; } @@ -82,7 +91,7 @@ class RelateType extends BaseFunction implements if (is_array($foreignId)) { foreach ($foreignId as $itemId) { - $relation->relateById($itemId); + $relation->relateById($itemId, $columnData); } return true; @@ -92,7 +101,7 @@ class RelateType extends BaseFunction implements $this->throwError("foreignId type is wrong."); } - $relation->relateById($foreignId); + $relation->relateById($foreignId, $columnData); return true; } diff --git a/tests/integration/Espo/Core/Formula/FormulaTest.php b/tests/integration/Espo/Core/Formula/FormulaTest.php index 8fd9134ba6..8a0cee1a69 100644 --- a/tests/integration/Espo/Core/Formula/FormulaTest.php +++ b/tests/integration/Espo/Core/Formula/FormulaTest.php @@ -697,6 +697,29 @@ class FormulaTest extends BaseTestCase $this->assertEquals('test', $value); } + /** @noinspection PhpUnhandledExceptionInspection */ + public function testRecordRelateWithColumnData(): void + { + $fm = $this->getContainer()->getByClass(Manager::class); + $em = $this->getContainer()->getByClass(EntityManager::class); + + $a = $em->createEntity('Account'); + $c = $em->createEntity('Contact'); + + $script = " + \$data = object\\create(); + \$data['role'] = 'Tester'; + + record\\relate('Account', '{$a->getId()}', 'contacts', '{$c->getId()}', \$data); + "; + + $fm->run($script); + + $value = $em->getRelation($a, 'contacts')->getColumnById($c->getId(), 'role'); + + $this->assertEquals('Tester', $value); + } + public function testExtAccountFindByEmailAddress() { $fm = $this->getContainer()->get('formulaManager');