EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
optimistic concurrency control improvement and ref
Yuri Kuznetsov8d6e8f2610b9
diff --git a/application/Espo/Core/Record/ConcurrencyControl/Optimistic/Result.php b/application/Espo/Core/Record/ConcurrencyControl/Optimistic/Result.phpnew file mode 100644index 0000000000..cc79a32ccc--- /dev/null+++ b/application/Espo/Core/Record/ConcurrencyControl/Optimistic/Result.php@@ -0,0 +1,46 @@+<?php+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2025 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko+ * Website: https://www.espocrm.com+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * The interactive user interfaces in modified source and object code versions+ * of this program must display Appropriate Legal Notices, as required under+ * Section 5 of the GNU Affero General Public License version 3.+ *+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.+ ************************************************************************/++namespace Espo\Core\Record\ConcurrencyControl\Optimistic;++use stdClass;++readonly class Result+{+ /**+ * @param string[] $fieldList Changed fields.+ * @param stdClass $values Previous values.+ * @param int $versionNumber A previous version number.+ */+ public function __construct(+ public array $fieldList,+ public stdClass $values,+ public int $versionNumber,+ ) {}+}diff --git a/application/Espo/Core/Record/ConcurrencyControl/OptimisticProcessor.php b/application/Espo/Core/Record/ConcurrencyControl/OptimisticProcessor.phpnew file mode 100644index 0000000000..91419f93b3--- /dev/null+++ b/application/Espo/Core/Record/ConcurrencyControl/OptimisticProcessor.php@@ -0,0 +1,106 @@+<?php+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2025 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko+ * Website: https://www.espocrm.com+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * The interactive user interfaces in modified source and object code versions+ * of this program must display Appropriate Legal Notices, as required under+ * Section 5 of the GNU Affero General Public License version 3.+ *+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.+ ************************************************************************/++namespace Espo\Core\Record\ConcurrencyControl;++use Espo\Core\Name\Field;+use Espo\Core\Record\ConcurrencyControl\Optimistic\Result;+use Espo\Core\Utils\FieldUtil;+use Espo\ORM\BaseEntity;+use Espo\ORM\Entity;+use Espo\ORM\EntityManager;++/**+ * @internal+ */+class OptimisticProcessor+{+ public function __construct(+ private EntityManager $entityManager,+ private FieldUtil $fieldUtil,+ ) {}++ public function process(Entity $entity, int $versionNumber): ?Result+ {+ $previousVersionNumber = $entity->getFetched(Field::VERSION_NUMBER);++ if ($previousVersionNumber === null) {+ return null;+ }++ if ($versionNumber === $previousVersionNumber) {+ return null;+ }++ $changedFieldList = [];++ $entityDefs = $this->entityManager+ ->getDefs()+ ->getEntity($entity->getEntityType());++ foreach ($entityDefs->getFieldNameList() as $field) {+ foreach ($this->fieldUtil->getActualAttributeList($entityDefs->getName(), $field) as $attribute) {+ if (+ $entity instanceof BaseEntity &&+ !$entity->isAttributeWritten($attribute)+ ) {+ continue;+ }++ if (!$entity->hasFetched($attribute)) {+ continue;+ }++ if ($entity->isAttributeChanged($attribute)) {+ $changedFieldList[] = $field;++ continue 2;+ }+ }+ }++ if ($changedFieldList === []) {+ return null;+ }++ $values = (object) [];++ foreach ($changedFieldList as $field) {+ foreach ($this->fieldUtil->getAttributeList($entityDefs->getName(), $field) as $attribute) {+ $values->$attribute = $entity->getFetched($attribute);+ }+ }++ return new Result(+ fieldList: $changedFieldList,+ values: $values,+ versionNumber: $previousVersionNumber,+ );+ }+}diff --git a/application/Espo/Core/Record/Service.php b/application/Espo/Core/Record/Service.phpindex 516b98422e..f6d59c9a9e 100644--- a/application/Espo/Core/Record/Service.php+++ b/application/Espo/Core/Record/Service.php@@ -46,6 +46,7 @@ use Espo\Core\ORM\Repository\Option\SaveOption;use Espo\Core\Record\Access\LinkCheck;use Espo\Core\Record\ActionHistory\Action;use Espo\Core\Record\ActionHistory\ActionLogger;+use Espo\Core\Record\ConcurrencyControl\OptimisticProcessor;use Espo\Core\Record\Defaults\Populator as DefaultsPopulator;use Espo\Core\Record\Defaults\PopulatorFactory as DefaultsPopulatorFactory;use Espo\Core\Record\Formula\Processor as FormulaProcessor;@@ -532,39 +533,20 @@ class Service implements Crud,* @throws Conflict* @noinspection PhpDocSignatureInspection*/- protected function processConcurrencyControl(Entity $entity, stdClass $data, int $versionNumber): void+ private function processConcurrencyControl(Entity $entity, int $versionNumber): void{- if ($entity->get(self::ATTRIBUTE_VERSION_NUMBER) === null) {- return;- }-- if ($versionNumber === $entity->get(self::ATTRIBUTE_VERSION_NUMBER)) {- return;- }-- $attributeList = array_keys(get_object_vars($data));+ // @todo Use a bound interface.+ $processor = $this->injectableFactory->create(OptimisticProcessor::class);- $notMatchingAttributeList = [];-- foreach ($attributeList as $attribute) {- if ($entity->get($attribute) !== $data->$attribute) {- $notMatchingAttributeList[] = $attribute;- }- }+ $result = $processor->process($entity, $versionNumber);- if (empty($notMatchingAttributeList)) {+ if (!$result) {return;}- $values = (object) [];-- foreach ($notMatchingAttributeList as $attribute) {- $values->$attribute = $entity->get($attribute);- }-$responseData = (object) [- 'values' => $values,- 'versionNumber' => $entity->get(self::ATTRIBUTE_VERSION_NUMBER),+ 'values' => $result->values,+ 'versionNumber' => $result->versionNumber,];throw ConflictSilent::createWithBody('modified', Json::encode($responseData));@@ -718,12 +700,12 @@ class Service implements Crud,throw new ForbiddenSilent("No edit access.");}+ $entity->set($data);+if ($params->getVersionNumber() !== null) {- $this->processConcurrencyControl($entity, $data, $params->getVersionNumber());+ $this->processConcurrencyControl($entity, $params->getVersionNumber());}- $entity->set($data);-$this->getRecordHookManager()->processEarlyBeforeUpdate($entity, $params);$this->processValidation($entity, $data);