This commit is contained in:
Yuri Kuznetsov
2024-11-15 13:08:10 +02:00
parent d8b3b5361a
commit 4eb375c1cf
22 changed files with 95 additions and 57 deletions

View File

@@ -32,6 +32,7 @@ namespace Espo\Core\Formula;
use Espo\Core\ORM\Defs\AttributeParam;
use Espo\Entities\EmailAddress;
use Espo\Entities\PhoneNumber;
use Espo\ORM\Defs\Params\AttributeParam as OrmAttributeParam;
use Espo\ORM\Entity;
use Espo\Core\ORM\Entity as CoreEntity;
use Espo\ORM\EntityManager;
@@ -101,7 +102,7 @@ class AttributeFetcher
{
if ($entity->getAttributeParam($attribute, 'isParentName')) {
/** @var ?string $relationName */
$relationName = $entity->getAttributeParam($attribute, 'relation');
$relationName = $entity->getAttributeParam($attribute, OrmAttributeParam::RELATION);
if ($relationName) {
$entity->loadParentNameField($relationName);
@@ -112,7 +113,7 @@ class AttributeFetcher
if ($entity->getAttributeParam($attribute, AttributeParam::IS_LINK_MULTIPLE_ID_LIST)) {
/** @var ?string $relationName */
$relationName = $entity->getAttributeParam($attribute, 'relation');
$relationName = $entity->getAttributeParam($attribute, OrmAttributeParam::RELATION);
if ($relationName) {
$entity->loadLinkMultipleField($relationName);

View File

@@ -32,6 +32,7 @@ namespace Espo\Core\Select\Text;
use Espo\Core\Utils\Metadata;
use Espo\ORM\Defs;
use Espo\ORM\Defs\Params\AttributeParam;
use Espo\ORM\Defs\Params\FieldParam;
class MetadataProvider
@@ -150,6 +151,6 @@ class MetadataProvider
return $this->ormDefs
->getEntity($entityType)
->getAttribute($attribute)
->getParam('relation');
->getParam(AttributeParam::RELATION);
}
}

View File

@@ -41,6 +41,7 @@ use Espo\Core\Utils\Metadata;
use Espo\Entities\ArrayValue;
use Espo\Entities\User;
use Espo\ORM\Defs as ORMDefs;
use Espo\ORM\Defs\Params\AttributeParam;
use Espo\ORM\Entity;
use Espo\ORM\Name\Attribute;
use Espo\ORM\Query\Part\Condition as Cond;
@@ -500,8 +501,8 @@ class ItemGeneralConverter implements ItemConverter
if ($isForeign) {
if ($isForeignType) {
$arrayLink = $entityDefs->getAttribute($attribute)->getParam('relation');
$arrayAttribute = $entityDefs->getAttribute($attribute)->getParam('foreign');
$arrayLink = $entityDefs->getAttribute($attribute)->getParam(AttributeParam::RELATION);
$arrayAttribute = $entityDefs->getAttribute($attribute)->getParam(AttributeParam::FOREIGN);
} else {
[$arrayLink, $arrayAttribute] = explode('.', $attribute);
}

View File

@@ -30,6 +30,7 @@
namespace Espo\Core\Select\Where;
use Espo\Core\Select\Where\Item\Type;
use Espo\ORM\Defs\Params\AttributeParam;
use Espo\ORM\EntityManager;
use Espo\ORM\Entity;
use Espo\ORM\BaseEntity;
@@ -192,7 +193,7 @@ class Scanner
$attributeType = $seed->getAttributeType($attribute);
if ($attributeType === Entity::FOREIGN) {
$relation = $this->getAttributeParam($seed, $attribute, 'relation');
$relation = $this->getAttributeParam($seed, $attribute, AttributeParam::RELATION);
if ($relation) {
$queryBuilder->leftJoin($relation);

View File

@@ -85,8 +85,8 @@ class Converter
'autoincrement' => 'autoincrement',
'entity' => 'entity',
FieldParam::NOT_STORABLE => AttributeParam::NOT_STORABLE,
'link' => 'relation',
'field' => 'foreign', // @todo Change 'foreign' to 'field'.
'link' => AttributeParam::RELATION,
'field' => AttributeParam::FOREIGN,
'unique' => 'unique',
'index' => 'index',
FieldParam::DEFAULT => AttributeParam::DEFAULT,
@@ -350,8 +350,8 @@ class Converter
{
$params = $data[$entityType][EntityParam::ATTRIBUTES][$attribute] ?? [];
$foreign = $params['foreign'] ?? null;
$relation = $params['relation'] ?? null;
$foreign = $params[AttributeParam::FOREIGN] ?? null;
$relation = $params[AttributeParam::RELATION] ?? null;
if (!$foreign || !$relation) {
return null;
@@ -359,7 +359,7 @@ class Converter
$relationParams = $data[$entityType][EntityParam::RELATIONS][$relation] ?? [];
$foreignEntityType = $relationParams['entity'] ?? null;
$foreignEntityType = $relationParams[RelationParam::ENTITY] ?? null;
if (!$foreignEntityType) {
return null;

View File

@@ -29,13 +29,16 @@
namespace Espo\Core\Utils\Database\Orm\FieldConverters;
use Espo\Core\Name\Field;
use Espo\Core\Utils\Database\Orm\Defs\AttributeDefs;
use Espo\Core\Utils\Database\Orm\Defs\EntityDefs;
use Espo\Core\Utils\Database\Orm\Defs\RelationDefs;
use Espo\Core\Utils\Database\Orm\FieldConverter;
use Espo\Entities\Attachment;
use Espo\ORM\Defs\FieldDefs;
use Espo\ORM\Defs\Params\AttributeParam;
use Espo\ORM\Defs\Params\RelationParam;
use Espo\ORM\Name\Attribute;
use Espo\ORM\Type\AttributeType;
use Espo\ORM\Type\RelationType;
@@ -72,15 +75,15 @@ class File implements FieldConverter
if (!$fieldDefs->isNotStorable()) {
$nameDefs = $nameDefs->withParamsMerged([
'relation' => $name,
'foreign' => 'name',
AttributeParam::RELATION => $name,
AttributeParam::FOREIGN => Field::NAME,
]);
$relationDefs = RelationDefs::create($name)
->withType(RelationType::BELONGS_TO)
->withForeignEntityType(Attachment::ENTITY_TYPE)
->withKey($idName)
->withForeignKey('id')
->withForeignKey(Attribute::ID)
->withParam(RelationParam::FOREIGN, null);
}

View File

@@ -71,7 +71,7 @@ class LinkParent implements FieldConverter
->withType(AttributeType::VARCHAR)
->withNotStorable()
->withParamsMerged([
'relation' => $name,
AttributeParam::RELATION => $name,
'isParentName' => true,
'attributeRole' => 'name',
'fieldType' => FieldType::LINK_PARENT,

View File

@@ -33,6 +33,8 @@ use Espo\Core\Utils\Database\Orm\Defs\AttributeDefs;
use Espo\Core\Utils\Database\Orm\Defs\EntityDefs;
use Espo\Core\Utils\Database\Orm\Defs\RelationDefs;
use Espo\Core\Utils\Database\Orm\LinkConverter;
use Espo\ORM\Defs\Params\AttributeParam;
use Espo\ORM\Defs\Params\RelationParam;
use Espo\ORM\Defs\RelationDefs as LinkDefs;
use Espo\ORM\Type\AttributeType;
use Espo\ORM\Type\RelationType;
@@ -47,7 +49,7 @@ class BelongsTo implements LinkConverter
$noIndex = $linkDefs->getParam('noIndex');
$noForeignName = $linkDefs->getParam('noForeignName');
$foreignName = $linkDefs->getParam('foreignName') ?? 'name';
$noJoin = $linkDefs->getParam('noJoin');
$noJoin = $linkDefs->getParam(RelationParam::NO_JOIN);
$idName = $name . 'Id';
$nameName = $name . 'Name';
@@ -63,8 +65,8 @@ class BelongsTo implements LinkConverter
->withForeignKey('id')
->withForeignRelationName($foreignRelationName);
if ($linkDefs->getParam('deferredLoad')) {
$relationDefs = $relationDefs->withParam('deferredLoad', true);
if ($linkDefs->getParam(RelationParam::DEFERRED_LOAD)) {
$relationDefs = $relationDefs->withParam(RelationParam::DEFERRED_LOAD, true);
}
$nameAttributeDefs = !$noForeignName ?
@@ -73,13 +75,13 @@ class BelongsTo implements LinkConverter
AttributeDefs::create($nameName)
->withType(AttributeType::VARCHAR)
->withNotStorable()
->withParam('relation', $name)
->withParam('foreign', $foreignName) :
->withParam(AttributeParam::RELATION, $name)
->withParam(AttributeParam::FOREIGN, $foreignName) :
AttributeDefs::create($nameName)
->withType(AttributeType::FOREIGN)
->withNotStorable(true) // Used to be false before v7.4.
->withParam('relation', $name)
->withParam('foreign', $foreignName)
->withNotStorable() // Used to be false before v7.4.
->withParam(AttributeParam::RELATION, $name)
->withParam(AttributeParam::FOREIGN, $foreignName)
) : null;
$entityDefs = EntityDefs::create()

View File

@@ -34,6 +34,7 @@ use Espo\Core\Utils\Database\Orm\Defs\EntityDefs;
use Espo\Core\Utils\Database\Orm\Defs\RelationDefs;
use Espo\Core\Utils\Database\Orm\LinkConverter;
use Espo\ORM\Defs\Params\AttributeParam;
use Espo\ORM\Defs\Params\RelationParam;
use Espo\ORM\Defs\RelationDefs as LinkDefs;
use Espo\ORM\Type\AttributeType;
use Espo\ORM\Type\RelationType;
@@ -58,8 +59,8 @@ class BelongsToParent implements LinkConverter
->withKey($idName)
->withForeignRelationName($foreignRelationName);
if ($linkDefs->getParam('deferredLoad')) {
$relationDefs = $relationDefs->withParam('deferredLoad', true);
if ($linkDefs->getParam(RelationParam::DEFERRED_LOAD)) {
$relationDefs = $relationDefs->withParam(RelationParam::DEFERRED_LOAD, true);
}
return EntityDefs::create()

View File

@@ -33,6 +33,7 @@ use Espo\Core\Utils\Database\Orm\Defs\AttributeDefs;
use Espo\Core\Utils\Database\Orm\Defs\EntityDefs;
use Espo\Core\Utils\Database\Orm\Defs\RelationDefs;
use Espo\Core\Utils\Database\Orm\LinkConverter;
use Espo\ORM\Defs\Params\AttributeParam;
use Espo\ORM\Defs\RelationDefs as LinkDefs;
use Espo\ORM\Name\Attribute;
use Espo\ORM\Type\AttributeType;
@@ -55,16 +56,16 @@ class HasOne implements LinkConverter
$idAttributeDefs = AttributeDefs::create($idName)
->withType($noJoin ? AttributeType::VARCHAR : AttributeType::FOREIGN)
->withNotStorable()
->withParam('relation', $name)
->withParam('foreign', Attribute::ID);
->withParam(AttributeParam::RELATION, $name)
->withParam(AttributeParam::FOREIGN, Attribute::ID);
$nameAttributeDefs = !$noForeignName ?
(
AttributeDefs::create($nameName)
->withType($noJoin ? AttributeType::VARCHAR : AttributeType::FOREIGN)
->withNotStorable()
->withParam('relation', $name)
->withParam('foreign', $foreignName)
->withParam(AttributeParam::RELATION, $name)
->withParam(AttributeParam::FOREIGN, $foreignName)
) : null;
$relationDefs = RelationDefs::create($name)

View File

@@ -32,6 +32,7 @@ namespace Espo\Hooks\Common;
use Espo\Core\Hook\Hook\AfterSave;
use Espo\Core\ORM\Repository\Option\SaveOption;
use Espo\ORM\Defs;
use Espo\ORM\Defs\Params\AttributeParam;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use Espo\ORM\Repository\Option\SaveOptions;
@@ -62,7 +63,8 @@ class ForeignFields implements AfterSave
$foreignList = array_filter(
$entity->getAttributeList(), fn ($it) => $entity->getAttributeType($it) === AttributeType::FOREIGN);
$relationList = array_map(fn ($it) => $defs->getAttribute($it)->getParam('relation'), $foreignList);
$relationList = array_map(
fn ($it) => $defs->getAttribute($it)->getParam(AttributeParam::RELATION), $foreignList);
$relationList = array_filter($relationList, fn ($it) => $entity->isAttributeChanged($it . 'Id'));
$relationList = array_values($relationList);

View File

@@ -457,8 +457,8 @@ class BaseEntity implements Entity
$type = $this->getAttributeType($attribute);
return $type === AttributeType::FOREIGN &&
$this->getAttributeParam($attribute, 'relation') === substr($attribute, 0, -2) &&
$this->getAttributeParam($attribute, 'foreign') === Attribute::ID &&
$this->getAttributeParam($attribute, AttributeParam::RELATION) === substr($attribute, 0, -2) &&
$this->getAttributeParam($attribute, AttributeParam::FOREIGN) === Attribute::ID &&
str_ends_with($attribute, 'Id');
}
@@ -583,8 +583,8 @@ class BaseEntity implements Entity
return null;
}
$relation = $entityDefs->getAttribute($attribute)->getParam('relation');
$foreign = $entityDefs->getAttribute($attribute)->getParam('foreign');
$relation = $entityDefs->getAttribute($attribute)->getParam(AttributeParam::RELATION);
$foreign = $entityDefs->getAttribute($attribute)->getParam(AttributeParam::FOREIGN);
if (!$relation) {
return null;

View File

@@ -63,4 +63,14 @@ class AttributeParam
* A default value.
*/
public const DEFAULT = 'default';
/**
* A relation. For foreign attributes.
*/
public const RELATION = 'relation';
/**
* A foreign attribute name. For foreign attributes.
*/
public const FOREIGN = 'foreign';
}

View File

@@ -81,7 +81,16 @@ class RelationParam
/**
* Middle keys.
* @todo
*/
public const MID_KEYS = 'midKeys';
/**
* No join.
*/
public const NO_JOIN = 'noJoin';
/**
* Deferred load.
*/
public const DEFERRED_LOAD = 'deferredLoad';
}

View File

@@ -2069,14 +2069,14 @@ abstract class BaseQueryComposer implements QueryComposer
if (
$entity->getAttributeType($field) == AttributeType::FOREIGN &&
$this->getAttributeParam($entity, $field, 'relation')
$this->getAttributeParam($entity, $field, AttributeParam::RELATION)
) {
$relationsToJoin[] = $this->getAttributeParam($entity, $field, 'relation');
$relationsToJoin[] = $this->getAttributeParam($entity, $field, AttributeParam::RELATION);
} else if (
$this->getAttributeParam($entity, $field, 'fieldType') == FieldType::LINK_ONE &&
$this->getAttributeParam($entity, $field, 'relation')
$this->getAttributeParam($entity, $field, AttributeParam::RELATION)
) {
$relationsToJoin[] = $this->getAttributeParam($entity, $field, 'relation');
$relationsToJoin[] = $this->getAttributeParam($entity, $field, AttributeParam::RELATION);
}
}
}
@@ -2088,7 +2088,7 @@ abstract class BaseQueryComposer implements QueryComposer
continue;
}
if ($this->getRelationParam($entity, $relationName, 'noJoin')) {
if ($this->getRelationParam($entity, $relationName, RelationParam::NO_JOIN)) {
continue;
}
@@ -2394,13 +2394,13 @@ abstract class BaseQueryComposer implements QueryComposer
switch ($attributeType) {
case Entity::FOREIGN:
$relationName = $this->getAttributeParam($entity, $attribute, 'relation');
$relationName = $this->getAttributeParam($entity, $attribute, AttributeParam::RELATION);
if (!$relationName) {
return null;
}
$foreign = $this->getAttributeParam($entity, $attribute, 'foreign');
$foreign = $this->getAttributeParam($entity, $attribute, AttributeParam::FOREIGN);
if (is_array($foreign)) {
$wsCount = 0;
@@ -2709,8 +2709,8 @@ abstract class BaseQueryComposer implements QueryComposer
if ($attributeType === Entity::FOREIGN) {
// @todo Add a test.
$relationName = $this->getAttributeParam($entity, $attribute, 'relation');
$foreign = $this->getAttributeParam($entity, $attribute, 'foreign');
$relationName = $this->getAttributeParam($entity, $attribute, AttributeParam::RELATION);
$foreign = $this->getAttributeParam($entity, $attribute, AttributeParam::FOREIGN);
if (!$relationName) {
throw new RuntimeException("No 'relation' param for field $entityType.$attribute.");
@@ -3276,7 +3276,7 @@ abstract class BaseQueryComposer implements QueryComposer
}
$relTable = $this->toDb(
$this->getRelationParam($entity, $relationName, 'relationName')
$this->getRelationParam($entity, $relationName, RelationParam::RELATION_NAME)
);
$distantTable = $this->toDb($foreignEntityType);

View File

@@ -31,6 +31,7 @@ namespace Espo\ORM\Relation;
use Espo\ORM\BaseEntity;
use Espo\ORM\Defs\Defs;
use Espo\ORM\Defs\Params\RelationParam;
use Espo\ORM\Entity;
use Espo\ORM\EntityCollection;
use Espo\ORM\EntityManager;
@@ -311,7 +312,7 @@ class RDBRelations implements Relations
->getEntity($this->entity->getEntityType())
->getRelation($relation);
if (!$defs->getParam('deferredLoad')) {
if (!$defs->getParam(RelationParam::DEFERRED_LOAD)) {
return null;
}

View File

@@ -159,7 +159,7 @@ class RDBRelationSelectBuilder
{
$transformedWhere = [];
$middleName = lcfirst($this->getRelationParam('relationName'));
$middleName = lcfirst($this->getRelationParam(RelationParam::RELATION_NAME));
foreach ($where as $key => $value) {
$transformedKey = $key;
@@ -447,7 +447,7 @@ class RDBRelationSelectBuilder
}
if (!$this->middleTableAlias) {
$middleName = $this->getRelationParam('relationName');
$middleName = $this->getRelationParam(RelationParam::RELATION_NAME);
if (!$middleName) {
throw new RuntimeException("No relation name.");

View File

@@ -32,6 +32,7 @@ namespace Espo\Tools\Export;
use Espo\Core\ORM\Defs\AttributeParam;
use Espo\Core\ORM\Repository\Option\SaveOption;
use Espo\Core\Record\Select\ApplierClassNameListProvider;
use Espo\ORM\Defs\Params\AttributeParam as OrmAttributeParam;
use Espo\Tools\Export\Collection as ExportCollection;
use Espo\Tools\Export\Processor\Params as ProcessorParams;
use Espo\ORM\Entity;
@@ -198,7 +199,7 @@ class Export
[$relation, $foreign] = str_contains($attribute, '_') ?
explode('_', $attribute) :
[
$this->getAttributeParam($entity, $attribute, 'relation'),
$this->getAttributeParam($entity, $attribute, OrmAttributeParam::RELATION),
$this->getAttributeParam($entity, $attribute, 'foreign')
];

View File

@@ -32,6 +32,7 @@ namespace Espo\Tools\Export\Format\Xlsx;
use Espo\Core\ORM\Entity as CoreEntity;
use Espo\Core\ORM\Type\FieldType;
use Espo\Core\Utils\Metadata;
use Espo\ORM\Defs\Params\RelationParam;
use Espo\ORM\Entity;
use Espo\Tools\Export\AdditionalFieldsLoader as AdditionalFieldsLoaderInterface;
@@ -62,7 +63,7 @@ class AdditionalFieldsLoader implements AdditionalFieldsLoaderInterface
(
(
$entity->getRelationType($link) === Entity::BELONGS_TO &&
$entity->getRelationParam($link, 'noJoin')
$entity->getRelationParam($link, RelationParam::NO_JOIN)
) ||
$entity->getRelationType($link) === Entity::HAS_ONE
) &&

View File

@@ -31,6 +31,7 @@ namespace Espo\Tools\Export\Format\Xlsx;
use Espo\Core\ORM\Type\FieldType;
use Espo\Core\Utils\Metadata;
use Espo\ORM\Defs\Params\RelationParam;
use Espo\ORM\Entity;
use Espo\Tools\Export\Params;
use Espo\Tools\Export\Processor;
@@ -107,7 +108,7 @@ class ParamsHandler implements ProcessorParamsHandler
continue;
}
if ($linkType === Entity::BELONGS_TO && !empty($defs['noJoin'])) {
if ($linkType === Entity::BELONGS_TO && !empty($defs[RelationParam::NO_JOIN])) {
if ($this->metadata->get(['entityDefs', $entityType, 'fields', $link])) {
$linkList[] = $link;
}

View File

@@ -29,6 +29,7 @@
namespace Espo\Tools\Import;
use Espo\Core\Name\Field;
use Espo\Core\ORM\Type\FieldType;
use Espo\Core\PhoneNumber\Sanitizer as PhoneNumberSanitizer;
use Espo\Core\FieldValidation\Exceptions\ValidationError;
@@ -631,8 +632,8 @@ class Import
return;
}
$foreignAttribute = $entity->getAttributeParam($attribute, 'foreign');
$relation = $entity->getAttributeParam($attribute, 'relation');
$foreignAttribute = $entity->getAttributeParam($attribute, AttributeParam::FOREIGN);
$relation = $entity->getAttributeParam($attribute, AttributeParam::RELATION);
if (!$relation) {
return;
@@ -1366,7 +1367,7 @@ class Import
if (
$entity->getAttributeType($attribute) === Entity::FOREIGN &&
$entity->getAttributeParam($attribute, 'foreign') === 'name'
$entity->getAttributeParam($attribute, AttributeParam::FOREIGN) === Field::NAME
) {
$this->processForeignAttribute($entity, $attribute);
}
@@ -1400,7 +1401,7 @@ class Import
if (
$entity->getAttributeType($attribute) === Entity::FOREIGN &&
$entity->getAttributeParam($attribute, 'foreign') !== 'name' &&
$entity->getAttributeParam($attribute, AttributeParam::FOREIGN) !== Field::NAME &&
$this->getFieldParam($this->entityType, $attribute, 'relateOnImport')
) {
$this->processForeignAttribute($entity, $attribute);

View File

@@ -41,6 +41,7 @@ use Espo\Entities\Autofollow;
use Espo\Entities\User;
use Espo\Entities\Preferences;
use Espo\ORM\Defs\Params\RelationParam;
use Espo\ORM\EntityManager;
use Espo\ORM\Entity;
use Espo\ORM\Defs\RelationDefs;
@@ -601,7 +602,7 @@ class HookProcessor
$entityType = $entity->getEntityType();
$foreignEntityType = $foreignEntity->getEntityType();
$foreignLink = $entity->getRelationParam($link, 'foreign');
$foreignLink = $entity->getRelationParam($link, RelationParam::FOREIGN);
if (
!empty($options[self::OPTION_NO_STREAM]) ||
@@ -634,7 +635,7 @@ class HookProcessor
$entityType = $entity->getEntityType();
$foreignEntityType = $foreignEntity->getEntityType();
$foreignLink = $entity->getRelationParam($link, 'foreign');
$foreignLink = $entity->getRelationParam($link, RelationParam::FOREIGN);
if (
!empty($options[self::OPTION_NO_STREAM]) ||