diff --git a/application/Espo/Core/Formula/Functions/RecordGroup/ExistsType.php b/application/Espo/Core/Formula/Functions/RecordGroup/ExistsType.php index 72872e5980..779c51b8c0 100644 --- a/application/Espo/Core/Formula/Functions/RecordGroup/ExistsType.php +++ b/application/Espo/Core/Formula/Functions/RecordGroup/ExistsType.php @@ -34,7 +34,6 @@ use Espo\Core\Exceptions\Forbidden; use Espo\Core\Formula\ArgumentList; use Espo\Core\Formula\Exceptions\Error; use Espo\Core\Formula\Functions\BaseFunction; - use Espo\Core\Di; use Espo\Core\Formula\Functions\RecordGroup\Util\FindQueryUtil; diff --git a/application/Espo/Core/Formula/Functions/RecordGroup/FindOneType.php b/application/Espo/Core/Formula/Functions/RecordGroup/FindOneType.php index 817e81b318..3325dd14c2 100644 --- a/application/Espo/Core/Formula/Functions/RecordGroup/FindOneType.php +++ b/application/Espo/Core/Formula/Functions/RecordGroup/FindOneType.php @@ -32,17 +32,12 @@ namespace Espo\Core\Formula\Functions\RecordGroup; use Espo\Core\Exceptions\BadRequest; use Espo\Core\Exceptions\Forbidden; use Espo\Core\Formula\ArgumentList; -use Espo\Core\Formula\Exceptions\Error; use Espo\Core\Formula\Exceptions\Error as FormulaError; use Espo\Core\Formula\Functions\BaseFunction; use Espo\Core\Di; use Espo\Core\Formula\Functions\RecordGroup\Util\FindQueryUtil; -use Espo\Core\Select\Where\Item; -use Espo\Core\Utils\Json; use Espo\ORM\Name\Attribute; use Espo\ORM\Query\Part\Order; -use InvalidArgumentException; -use stdClass; /** * @noinspection PhpUnused diff --git a/application/Espo/Core/Formula/Functions/RecordGroup/FindRelatedManyType.php b/application/Espo/Core/Formula/Functions/RecordGroup/FindRelatedManyType.php index d4225985d2..972bfe5fd0 100644 --- a/application/Espo/Core/Formula/Functions/RecordGroup/FindRelatedManyType.php +++ b/application/Espo/Core/Formula/Functions/RecordGroup/FindRelatedManyType.php @@ -136,7 +136,13 @@ class FindRelatedManyType extends BaseFunction implements $relationType = $entity->getRelationParam($link, 'type'); - if (in_array($relationType, ['belongsTo', 'hasOne', 'belongsToParent'])) { + if ( + in_array($relationType, [ + RelationType::BELONGS_TO, + RelationType::HAS_ONE, + RelationType::BELONGS_TO_PARENT, + ]) + ) { $this->throwError("Not supported link type '$relationType'."); } diff --git a/application/Espo/Core/Formula/Functions/RecordGroup/FindRelatedOneType.php b/application/Espo/Core/Formula/Functions/RecordGroup/FindRelatedOneType.php index cb7c1c54d0..9962cd0491 100644 --- a/application/Espo/Core/Formula/Functions/RecordGroup/FindRelatedOneType.php +++ b/application/Espo/Core/Formula/Functions/RecordGroup/FindRelatedOneType.php @@ -32,16 +32,15 @@ namespace Espo\Core\Formula\Functions\RecordGroup; use Espo\Core\Exceptions\BadRequest; use Espo\Core\Exceptions\Forbidden; use Espo\Core\ORM\Entity as CoreEntity; - use Espo\ORM\Defs\Params\RelationParam; use Espo\ORM\Name\Attribute; -use Espo\Core\Formula\{ - Exceptions\Error, - Functions\BaseFunction, - ArgumentList, - Functions\RecordGroup\Util\FindQueryUtil}; - +use Espo\Core\Formula\ArgumentList; +use Espo\Core\Formula\Exceptions\Error; +use Espo\Core\Formula\Functions\BaseFunction; +use Espo\Core\Formula\Functions\RecordGroup\Util\FindQueryUtil; use Espo\Core\Di; +use Espo\ORM\Query\Part\Order; +use Espo\ORM\Type\RelationType; class FindRelatedOneType extends BaseFunction implements Di\EntityManagerAware, @@ -101,7 +100,13 @@ class FindRelatedOneType extends BaseFunction implements $relationType = $entity->getRelationParam($link, 'type'); - if (in_array($relationType, ['belongsTo', 'hasOne', 'belongsToParent'])) { + if ( + in_array($relationType, [ + RelationType::BELONGS_TO, + RelationType::HAS_ONE, + RelationType::BELONGS_TO_PARENT, + ]) + ) { $relatedEntity = $entityManager ->getRDBRepository($entityType) ->getRelation($entity, $link) @@ -122,7 +127,7 @@ class FindRelatedOneType extends BaseFunction implements $order = $metadata->get(['entityDefs', $entityType, 'collection', 'order']) ?? 'ASC'; } } else { - $order = $order ?? 'ASC'; + $order = $order ?? Order::ASC; } $foreignEntityType = $entity->getRelationParam($link, RelationParam::ENTITY); @@ -174,7 +179,7 @@ class FindRelatedOneType extends BaseFunction implements $queryBuilder->where($whereClause); } - if ($relationType === 'hasChildren') { + if ($relationType === RelationType::HAS_CHILDREN) { $queryBuilder->where([ $foreignLink . 'Id' => $entity->getId(), $foreignLink . 'Type' => $entity->getEntityType(), diff --git a/tests/integration/Espo/Core/Formula/FormulaTest.php b/tests/integration/Espo/Core/Formula/FormulaTest.php index a228c430b3..a2c1d539cd 100644 --- a/tests/integration/Espo/Core/Formula/FormulaTest.php +++ b/tests/integration/Espo/Core/Formula/FormulaTest.php @@ -36,6 +36,7 @@ use Espo\Core\Formula\Exceptions\UnsafeFunction; use Espo\Core\Formula\Manager; use Espo\Entities\User; use Espo\Modules\Crm\Entities\Account; +use Espo\Modules\Crm\Entities\Contact; use Espo\Modules\Crm\Entities\Meeting; use Espo\Modules\Crm\Entities\Opportunity; use Espo\ORM\EntityManager; @@ -1016,4 +1017,159 @@ class FormulaTest extends BaseTestCase /** @noinspection PhpUnhandledExceptionInspection */ $fm->runSafe($script); } + + /** + * @noinspection PhpUnhandledExceptionInspection + */ + public function testFilterWhere(): void + { + $em = $this->getEntityManager(); + $fm = $this->getContainer()->getByClass(Manager::class); + + $account1 = $em->createEntity(Account::ENTITY_TYPE, ['name' => 'a1']); + + $em->createEntity(Account::ENTITY_TYPE, ['name' => 'a2']); + + $contact1 = $em->createEntity(Contact::ENTITY_TYPE, [ + 'lastName' => 'c1_1', + 'accountId' => $account1->getId(), + ]); + + $em->createEntity(Contact::ENTITY_TYPE, [ + 'lastName' => 'c1_2', + 'accountId' => $account1->getId(), + ]); + + $em->createEntity(Opportunity::ENTITY_TYPE, [ + 'accountId' => $account1->getId(), + 'amount' => 100.0, + 'name' => 'o1_1', + ]); + + $em->createEntity(Opportunity::ENTITY_TYPE, [ + 'accountId' => $account1->getId(), + 'amount' => 150.0, + 'name' => 'o1_2', + ]); + + $script = " + \$where = object\create(\$w); + \$where['type'] = 'and'; + \$where['value'] = list( + ( + \$item1 = object\create(); + \$item1['type'] = 'equals'; + \$item1['attribute'] = 'name'; + \$item1['value'] = 'a1'; + \$item1; + ) + ); + + record\\findMany('Account', 2, null, null, \$where); + "; + $result = $fm->run($script); + $this->assertEquals([$account1->getId()], $result); + + $script = " + \$where = object\create(\$w); + \$where['type'] = 'and'; + \$where['value'] = list( + ( + \$item1 = object\create(); + \$item1['type'] = 'equals'; + \$item1['attribute'] = 'name'; + \$item1['value'] = 'a1'; + \$item1; + ) + ); + + record\\findOne('Account', null, null, \$where); + "; + $result = $fm->run($script); + $this->assertEquals($account1->getId(), $result); + + $script = " + \$where = object\create(\$w); + \$where['type'] = 'and'; + \$where['value'] = list( + ( + \$item1 = object\create(); + \$item1['type'] = 'equals'; + \$item1['attribute'] = 'name'; + \$item1['value'] = 'a1'; + \$item1; + ) + ); + + record\\exists('Account', \$where); + "; + $result = $fm->run($script); + $this->assertTrue($result); + + $script = " + \$where = object\create(\$w); + \$where['type'] = 'and'; + \$where['value'] = list( + ( + \$item1 = object\create(); + \$item1['type'] = 'equals'; + \$item1['attribute'] = 'name'; + \$item1['value'] = 'a1'; + \$item1; + ) + ); + + record\\count('Account', \$where); + "; + $result = $fm->run($script); + $this->assertEquals(1, $result); + + $script = " + \$item = object\create(); + \$item['type'] = 'equals'; + \$item['attribute'] = 'lastName'; + \$item['value'] = 'c1_1'; + \$item; + + record\\findRelatedOne('Account', '{$account1->getId()}', 'contacts', null, null, \$item); + "; + $result = $fm->run($script); + $this->assertEquals($contact1->getId(), $result); + + $script = " + \$item = object\create(); + \$item['type'] = 'equals'; + \$item['attribute'] = 'lastName'; + \$item['value'] = 'c1_1'; + \$item; + + record\\findRelatedMany('Account', '{$account1->getId()}', 'contacts', 2, null, null, \$item); + "; + $result = $fm->run($script); + $this->assertEquals([$contact1->getId()], $result); + + $script = " + \$item = object\create(); + \$item['type'] = 'equals'; + \$item['attribute'] = 'lastName'; + \$item['value'] = 'c1_1'; + \$item; + + entity\\countRelated('contacts', \$item); + "; + $result = $fm->run($script, $account1); + $this->assertEquals(1, $result); + + $script = " + \$item = object\create(); + \$item['type'] = 'equals'; + \$item['attribute'] = 'name'; + \$item['value'] = 'o1_1'; + \$item; + + entity\\sumRelated('opportunities', 'amount', \$item); + "; + $result = $fm->run($script, $account1); + $this->assertEquals(100.0, $result); + } }