Skip to content

EspoCRM – Open Source CRM Application

default order in relations

Yuri Kuznetsov47b9d35153d0
diff --git a/application/Espo/Core/Utils/Database/Orm/RelationConverter.php b/application/Espo/Core/Utils/Database/Orm/RelationConverter.php
index 11d5c5f3a7..08fdbd9dc2 100644
--- a/application/Espo/Core/Utils/Database/Orm/RelationConverter.php
+++ b/application/Espo/Core/Utils/Database/Orm/RelationConverter.php
@@ -61,6 +61,12 @@ class RelationConverter
RelationParam::INDEXES,
];
+ /** @var string[] */
+ private $manyMergeParams = [
+ RelationParam::ORDER_BY,
+ RelationParam::ORDER,
+ ];
+
public function __construct(
private Metadata $metadata,
private InjectableFactory $injectableFactory,
@@ -112,7 +118,7 @@ class RelationConverter
$raw = $convertedEntityDefs->toAssoc();
if (isset($raw[EntityParam::RELATIONS][$name])) {
- $this->mergeParams($raw[EntityParam::RELATIONS][$name], $params, $foreignParams ?? []);
+ $this->mergeParams($raw[EntityParam::RELATIONS][$name], $params, $foreignParams ?? [], $linkType);
$this->correct($raw[EntityParam::RELATIONS][$name]);
}
@@ -172,9 +178,18 @@ class RelationConverter
* @param array<string, mixed> $params
* @param array<string, mixed> $foreignParams
*/
- private function mergeParams(array &$relationDefs, array $params, array $foreignParams): void
+ private function mergeParams(array &$relationDefs, array $params, array $foreignParams, string $linkType): void
{
- foreach ($this->mergeParams as $name) {
+ $mergeParams = $this->mergeParams;
+
+ if (
+ $linkType === RelationType::HAS_MANY ||
+ $linkType === RelationType::HAS_CHILDREN
+ ) {
+ $mergeParams = array_merge($mergeParams, $this->manyMergeParams);
+ }
+
+ foreach ($mergeParams as $name) {
$additionalParam = $this->getMergedParam($name, $params, $foreignParams);
if ($additionalParam === null) {
@@ -227,6 +242,13 @@ class RelationConverter
*/
private function correct(array &$relationDefs): void
{
+ if (
+ isset($relationDefs[RelationParam::ORDER]) &&
+ is_string($relationDefs[RelationParam::ORDER])
+ ) {
+ $relationDefs[RelationParam::ORDER] = strtoupper($relationDefs[RelationParam::ORDER]);
+ }
+
if (!isset($relationDefs[RelationParam::ADDITIONAL_COLUMNS])) {
return;
}
diff --git a/application/Espo/ORM/Defs/Params/RelationParam.php b/application/Espo/ORM/Defs/Params/RelationParam.php
index 72e4d2a098..da94ad9e95 100644
--- a/application/Espo/ORM/Defs/Params/RelationParam.php
+++ b/application/Espo/ORM/Defs/Params/RelationParam.php
@@ -93,4 +93,18 @@ class RelationParam
* Deferred load.
*/
public const DEFERRED_LOAD = 'deferredLoad';
+
+ /**
+ * Default order by. Applied on the entity level.
+ *
+ * @since 9.2.5
+ */
+ public const ORDER_BY = 'orderBy';
+
+ /**
+ * Default order. Applied on the entity level.
+ *
+ * @since 9.2.5
+ */
+ public const ORDER = 'order';
}
diff --git a/application/Espo/ORM/Relation/RDBRelations.php b/application/Espo/ORM/Relation/RDBRelations.php
index 8fed528acf..e4004f1e71 100644
--- a/application/Espo/ORM/Relation/RDBRelations.php
+++ b/application/Espo/ORM/Relation/RDBRelations.php
@@ -32,12 +32,14 @@ namespace Espo\ORM\Relation;
use Espo\ORM\BaseEntity;
use Espo\ORM\Defs\Defs;
use Espo\ORM\Defs\Params\RelationParam;
+use Espo\ORM\Defs\RelationDefs;
use Espo\ORM\Entity;
use Espo\ORM\EntityCollection;
use Espo\ORM\EntityManager;
use Espo\ORM\Mapper\RDBMapper;
use Espo\ORM\Name\Attribute;
use Espo\ORM\Query\Part\Order;
+use Espo\ORM\Repository\RDBRelationSelectBuilder;
use Espo\ORM\Type\RelationType;
use LogicException;
use RuntimeException;
@@ -293,22 +295,11 @@ class RDBRelations implements Relations
->getEntity($this->getEntity()->getEntityType())
->getRelation($relation);
- $orderBy = null;
- $order = null;
+ $builder = $this->entityManager
+ ->getRelation($this->getEntity(), $relation)
+ ->createBuilder();
- if ($relationDefs->getParam('orderBy')) {
- $orderBy = $relationDefs->getParam('orderBy');
-
- if ($relationDefs->getParam('order')) {
- $order = strtoupper($relationDefs->getParam('order')) === Order::DESC ? Order::DESC : Order::ASC;
- }
- }
-
- $builder = $this->entityManager->getRelation($this->getEntity(), $relation);
-
- if ($orderBy) {
- $builder->order($orderBy, $order);
- }
+ $this->applyOrder($relationDefs, $builder);
$collection = $builder->find();
@@ -398,4 +389,21 @@ class RDBRelations implements Relations
return $foreignEntity;
}
+
+ private function applyOrder(RelationDefs $relationDefs, RDBRelationSelectBuilder $builder): void
+ {
+ $orderBy = $relationDefs->getParam(RelationParam::ORDER_BY);
+
+ if (!$orderBy) {
+ return;
+ }
+
+ $order = $relationDefs->getParam(RelationParam::ORDER);
+
+ if ($order !== null) {
+ $order = strtoupper($order) === Order::DESC ? Order::DESC : Order::ASC;
+ }
+
+ $builder->order($orderBy, $order);
+ }
}
diff --git a/application/Espo/ORM/Repository/RDBRelation.php b/application/Espo/ORM/Repository/RDBRelation.php
index e788b930fa..d5e314bff2 100644
--- a/application/Espo/ORM/Repository/RDBRelation.php
+++ b/application/Espo/ORM/Repository/RDBRelation.php
@@ -95,6 +95,16 @@ class RDBRelation
}
}
+ /**
+ * @return Builder<TEntity>
+ *
+ * @since 9.2.5
+ */
+ public function createBuilder(): Builder
+ {
+ return $this->createSelectBuilder();
+ }
+
/**
* Create a select builder.
*
diff --git a/tests/integration/Espo/ORM/RelationsTest.php b/tests/integration/Espo/ORM/RelationsTest.php
index 03c222e00a..dce0552ec8 100644
--- a/tests/integration/Espo/ORM/RelationsTest.php
+++ b/tests/integration/Espo/ORM/RelationsTest.php
@@ -29,8 +29,10 @@
namespace tests\integration\Espo\ORM;
+use Espo\Modules\Crm\Entities\Contact;
use Espo\Modules\Crm\Entities\Lead;
use Espo\Modules\Crm\Entities\Task;
+use Espo\ORM\Defs\Params\RelationParam;
use Espo\ORM\EntityCollection;
use Espo\Modules\Crm\Entities\Account;
use Espo\Modules\Crm\Entities\Opportunity;
@@ -232,4 +234,71 @@ class RelationsTest extends BaseTestCase
$this->assertEquals(null, $lead2->get('createdAccountId'));
}
+
+ public function testOrder(): void
+ {
+ $em = $this->getEntityManager();
+
+ $account1 = $em->createEntity(Account::ENTITY_TYPE, ['name' => 'a1']);
+ $account2 = $em->createEntity(Account::ENTITY_TYPE, ['name' => 'a2']);
+
+ $contact = $em->createEntity(Contact::ENTITY_TYPE);
+
+ $this->assertInstanceOf(Contact::class, $contact);
+
+ $em->getRelation($contact, 'accounts')->relate($account1);
+ $em->getRelation($contact, 'accounts')->relate($account2);
+
+ //
+
+ $metadata = $this->getMetadata();
+ $metadata->set('entityDefs', Contact::ENTITY_TYPE, [
+ 'links' => [
+ 'accounts' => [
+ RelationParam::ORDER_BY => 'name',
+ RelationParam::ORDER => 'desc',
+ ]
+ ]
+ ]);
+ $metadata->save();
+
+ $this->reCreateApplication();
+
+ $em = $this->getEntityManager();
+
+ $contact = $em->getRDBRepositoryByClass(Contact::class)->getById($contact->getId());
+ $this->assertNotNull($contact);
+
+ $accounts = $contact->getAccounts();
+
+ $this->assertCount(2, $accounts);
+ $this->assertEquals('a2', $accounts[0]->getName());
+ $this->assertEquals('a1', $accounts[1]->getName());
+
+ //
+
+ $metadata = $this->getMetadata();
+ $metadata->set('entityDefs', Contact::ENTITY_TYPE, [
+ 'links' => [
+ 'accounts' => [
+ RelationParam::ORDER_BY => 'name',
+ RelationParam::ORDER => 'asc',
+ ]
+ ]
+ ]);
+ $metadata->save();
+
+ $this->reCreateApplication();
+
+ $em = $this->getEntityManager();
+
+ $contact = $em->getRDBRepositoryByClass(Contact::class)->getById($contact->getId());
+ $this->assertNotNull($contact);
+
+ $accounts = $contact->getAccounts();
+
+ $this->assertCount(2, $accounts);
+ $this->assertEquals('a1', $accounts[0]->getName());
+ $this->assertEquals('a2', $accounts[1]->getName());
+ }
}