diff --git a/application/Espo/Core/Formula/Functions/RecordGroup/FindRelatedManyType.php b/application/Espo/Core/Formula/Functions/RecordGroup/FindRelatedManyType.php index 8eb5a1a70c..2ad0f6f2aa 100644 --- a/application/Espo/Core/Formula/Functions/RecordGroup/FindRelatedManyType.php +++ b/application/Espo/Core/Formula/Functions/RecordGroup/FindRelatedManyType.php @@ -33,15 +33,18 @@ use Espo\Core\ORM\Entity as CoreEntity; use Espo\Core\Formula\ArgumentList; use Espo\Core\Formula\Functions\BaseFunction; use Espo\Core\Di; +use Espo\Core\Select\Helpers\RandomStringGenerator; class FindRelatedManyType extends BaseFunction implements Di\EntityManagerAware, Di\SelectBuilderFactoryAware, - Di\MetadataAware + Di\MetadataAware, + Di\InjectableFactoryAware { use Di\EntityManagerSetter; use Di\SelectBuilderFactorySetter; use Di\MetadataSetter; + use Di\InjectableFactorySetter; public function process(ArgumentList $args) { @@ -180,10 +183,12 @@ class FindRelatedManyType extends BaseFunction implements ]); } else { + $alias = $foreignLink . $this->generateRandomString(); + $queryBuilder - ->join($foreignLink) + ->join($foreignLink, $alias) ->where([ - $foreignLink . '.id' => $entity->getId(), + $alias . '.id' => $entity->getId(), ]); } @@ -207,4 +212,11 @@ class FindRelatedManyType extends BaseFunction implements return $idList; } + + private function generateRandomString(): string + { + $generator = $this->injectableFactory->create(RandomStringGenerator::class); + + return $generator->generate(); + } } diff --git a/tests/integration/Espo/Core/Formula/FormulaTest.php b/tests/integration/Espo/Core/Formula/FormulaTest.php index dff39a853b..4620204117 100644 --- a/tests/integration/Espo/Core/Formula/FormulaTest.php +++ b/tests/integration/Espo/Core/Formula/FormulaTest.php @@ -387,29 +387,33 @@ class FormulaTest extends BaseTestCase public function testRecordFindRelatedMany() { - $fm = $this->getContainer()->get('formulaManager'); - $em = $this->getContainer()->get('entityManager'); + $fm = $this->getContainer()->getByClass(Manager::class); + $em = $this->getContainer()->getByClass(EntityManager::class); $a = $em->createEntity('Account', []); + $c1 = $em->createEntity('Contact', []); + $c2 = $em->createEntity('Contact', []); $o1 = $em->createEntity('Opportunity', [ 'accountId' => $a->getId(), 'stage' => 'Prospecting', 'name' => '1', + 'contactsIds' => [$c1->getId()], ]); $o2 = $em->createEntity('Opportunity', [ 'accountId' => $a->getId(), 'stage' => 'Closed Won', 'name' => '2', + 'contactsIds' => [$c1->getId()], ]); $o3 = $em->createEntity('Opportunity', [ 'accountId' => $a->getId(), 'stage' => 'Prospecting', 'name' => '3', + 'contactsIds' => [$c2->getId()], ]); - $ow1 = $em->createEntity('Opportunity', []); - + $em->createEntity('Opportunity', []); $script = "record\\findRelatedMany('Account', '".$a->getId()."', 'opportunities', 2, null, null, 'open')"; $result = $fm->run($script); @@ -436,6 +440,11 @@ class FormulaTest extends BaseTestCase $result = $fm->run($script); $this->assertIsArray($result); $this->assertEquals([$o1->getId(), $o3->getId()], $result); + + $script = "record\\findRelatedMany('Contact', '{$c1->getId()}', 'opportunities', 10, 'name')"; + $result = $fm->run($script); + $this->assertIsArray($result); + $this->assertEquals([$o1->getId(), $o2->getId()], $result); } public function testRecordAttribute()