Skip to content

EspoCRM – Open Source CRM Application

Ref

Yuriib4ceb64f7322
diff --git a/application/Espo/Entities/ArrayValue.php b/application/Espo/Entities/ArrayValue.php
index 59589d6042..8a436b850e 100644
--- a/application/Espo/Entities/ArrayValue.php
+++ b/application/Espo/Entities/ArrayValue.php
@@ -29,7 +29,16 @@
namespace Espo\Entities;
-class ArrayValue extends \Espo\Core\ORM\Entity
+use Espo\Core\ORM\Entity;
+
+class ArrayValue extends Entity
{
- public const ENTITY_TYPE = 'ArrayValue';
+ public const string ENTITY_TYPE = 'ArrayValue';
+
+ public const string FIELD_VALUE = 'value';
+
+ public function getValue(): ?string
+ {
+ return $this->get(self::FIELD_VALUE);
+ }
}
diff --git a/application/Espo/Repositories/ArrayValue.php b/application/Espo/Repositories/ArrayValue.php
index 0e54a7e4a5..5aba30ff7b 100644
--- a/application/Espo/Repositories/ArrayValue.php
+++ b/application/Espo/Repositories/ArrayValue.php
@@ -35,7 +35,6 @@ use Espo\ORM\Defs\Params\AttributeParam;
use Espo\ORM\Defs\Params\FieldParam;
use Espo\ORM\Entity;
use Espo\Core\Repositories\Database;
-
use Espo\ORM\Name\Attribute;
use RuntimeException;
use LogicException;
@@ -45,7 +44,7 @@ use LogicException;
*/
class ArrayValue extends Database
{
- private const ITEM_MAX_LENGTH = 100;
+ private const int ITEM_MAX_LENGTH = 100;
public function storeEntityAttribute(CoreEntity $entity, string $attribute, bool $populateMode = false): void
{
@@ -53,15 +52,11 @@ class ArrayValue extends Database
throw new LogicException("ArrayValue: Can't store non array attribute.");
}
- if ($entity->getAttributeParam($attribute, AttributeParam::NOT_STORABLE)) {
- return;
- }
-
- if (!$entity->getAttributeParam($attribute, 'storeArrayValues')) {
- return;
- }
-
- if (!$entity->has($attribute)) {
+ if (
+ $entity->getAttributeParam($attribute, AttributeParam::NOT_STORABLE) ||
+ !$entity->getAttributeParam($attribute, 'storeArrayValues') ||
+ !$entity->has($attribute)
+ ) {
return;
}
@@ -72,21 +67,48 @@ class ArrayValue extends Database
}
if (!is_array($valueList)) {
- throw new RuntimeException("ArrayValue: Bad value passed to JSON_ARRAY attribute {$attribute}.");
+ throw new RuntimeException("ArrayValue: Bad value passed to JSON_ARRAY attribute '$attribute'.");
}
$valueList = array_unique($valueList);
- $toSkipValueList = [];
-
- $isTransaction = false;
if (!$entity->isNew() && !$populateMode) {
- $this->entityManager->getTransactionManager()->start();
+ $this->entityManager->getTransactionManager()
+ ->run(
+ fn () => $this->storeInternal(
+ entity: $entity,
+ populateMode: $populateMode,
+ attribute: $attribute,
+ valueList: $valueList,
+ )
+ );
+
+ return;
+ }
- $isTransaction = true;
+ $this->storeInternal(
+ entity: $entity,
+ populateMode: $populateMode,
+ attribute: $attribute,
+ valueList: $valueList,
+ );
+ }
+ /**
+ * @param string[] $valueList
+ */
+ private function storeInternal(
+ CoreEntity $entity,
+ bool $populateMode,
+ string $attribute,
+ array $valueList,
+ ): void {
+
+ $toSkipValueList = [];
+
+ if (!$entity->isNew() && !$populateMode) {
$existingList = $this
- ->select([Attribute::ID, 'value'])
+ ->select([Attribute::ID, ArrayValueEntity::FIELD_VALUE])
->where([
'entityType' => $entity->getEntityType(),
'entityId' => $entity->getId(),
@@ -96,20 +118,20 @@ class ArrayValue extends Database
->find();
foreach ($existingList as $existing) {
- if (!in_array($existing->get('value'), $valueList)) {
+ if (!in_array($existing->getValue(), $valueList)) {
$this->deleteFromDb($existing->getId());
continue;
}
- $toSkipValueList[] = $existing->get('value');
+ $toSkipValueList[] = $existing->getValue();
}
}
$itemMaxLength = $this->entityManager
->getDefs()
->getEntity(ArrayValueEntity::ENTITY_TYPE)
- ->getField('value')
+ ->getField(ArrayValueEntity::FIELD_VALUE)
->getParam(FieldParam::MAX_LENGTH) ?? self::ITEM_MAX_LENGTH;
foreach ($valueList as $value) {
@@ -127,43 +149,42 @@ class ArrayValue extends Database
$arrayValue = $this->getNew();
- $arrayValue->set([
+ $arrayValue->setMultiple([
'entityType' => $entity->getEntityType(),
'entityId' => $entity->getId(),
'attribute' => $attribute,
- 'value' => $value,
+ ArrayValueEntity::FIELD_VALUE => $value,
]);
$this->save($arrayValue);
}
-
- if ($isTransaction) {
- $this->entityManager->getTransactionManager()->commit();
- }
}
public function deleteEntityAttribute(CoreEntity $entity, string $attribute): void
{
if (!$entity->hasId()) {
- throw new LogicException("ArrayValue: Can't delete {$attribute} w/o id given.");
+ throw new LogicException("ArrayValue: Can't delete '$attribute' w/o id given.");
}
- $this->entityManager->getTransactionManager()->start();
+ $this->entityManager->getTransactionManager()
+ ->run(fn () => $this->deleteEntityAttributeInternal($entity, $attribute));
+ }
+
+ private function deleteEntityAttributeInternal(CoreEntity $entity, string $attribute): void
+ {
$list = $this
->select([Attribute::ID])
+ ->forUpdate()
->where([
'entityType' => $entity->getEntityType(),
'entityId' => $entity->getId(),
'attribute' => $attribute,
])
- ->forUpdate()
->find();
foreach ($list as $arrayValue) {
$this->deleteFromDb($arrayValue->getId());
}
-
- $this->entityManager->getTransactionManager()->commit();
}
}