findRelationMany function random alias

This commit is contained in:
Yuri Kuznetsov
2024-01-11 19:24:53 +02:00
parent e12e7ec95a
commit dcdc94365e
2 changed files with 28 additions and 7 deletions
@@ -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();
}
}
@@ -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()