From b3ef634d214ac86cb1e224a7b49e970b7f1bd224 Mon Sep 17 00:00:00 2001 From: Pierre Goiffon Date: Tue, 13 Jun 2023 10:15:53 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B01150=20SelectObjectField=20:=20factorize?= =?UTF-8?q?=20DBObjectSet=20retrieval=20between=20Validate=20and=20VerifyC?= =?UTF-8?q?urrentValue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sources/Form/Field/SelectObjectField.php | 37 ++++++++++++------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/sources/Form/Field/SelectObjectField.php b/sources/Form/Field/SelectObjectField.php index 1d70a437d..63058268b 100644 --- a/sources/Form/Field/SelectObjectField.php +++ b/sources/Form/Field/SelectObjectField.php @@ -253,13 +253,7 @@ class SelectObjectField extends Field if ($this->GetReadOnly() === false) { $sCurrentValueForExtKey = $this->currentValue; if (utils::IsNotNullOrEmptyString($sCurrentValueForExtKey) && ($sCurrentValueForExtKey !== 0)) { - $oSearchForExistingCurrentValue = $this->oSearch->DeepClone(); - $oSearchForExistingCurrentValue->AddCondition('id', $sCurrentValueForExtKey, '='); - $oCheckIdAgainstCurrentValueExpression = new BinaryExpression( - new FieldExpression('id', $oSearchForExistingCurrentValue->GetClassAlias()), '=', new ScalarExpression($sCurrentValueForExtKey) - ); - $oSearchForExistingCurrentValue->AddConditionExpression($oCheckIdAgainstCurrentValueExpression); - $oSetForExistingCurrentValue = new DBObjectSet($oSearchForExistingCurrentValue); + $oSetForExistingCurrentValue = $this->GetObjectsSet(); $iObjectsCount = $oSetForExistingCurrentValue->CountWithLimit(1); if ($iObjectsCount === 0) { @@ -282,20 +276,27 @@ class SelectObjectField extends Field * * @throws \CoreException */ - public function VerifyCurrentValue(bool $bAlways = false) - { - if (!$this->GetReadOnly() || $bAlways) - { - $oValuesScope = $this->GetSearch()->DeepClone(); - $oBinaryExp = new BinaryExpression(new FieldExpression('id', $oValuesScope->GetClassAlias()), '=', - new ScalarExpression($this->currentValue)); - $oValuesScope->AddConditionExpression($oBinaryExp); - $oValuesSet = new DBObjectSet($oValuesScope); + public function VerifyCurrentValue(bool $bAlways = false) { + if (!$this->GetReadOnly() || $bAlways) { + $oValuesSet = $this->GetObjectsSet(); - if ($oValuesSet->Count() === 0) - { + if ($oValuesSet->Count() === 0) { $this->currentValue = null; } } } + + final protected function GetObjectsSet() { + $sCurrentValueForExtKey = $this->currentValue; + + $oSearchForExistingCurrentValue = $this->oSearch->DeepClone(); + $oCheckIdAgainstCurrentValueExpression = new BinaryExpression( + new FieldExpression('id', $oSearchForExistingCurrentValue->GetClassAlias()), + '=', + new ScalarExpression($sCurrentValueForExtKey) + ); + $oSearchForExistingCurrentValue->AddConditionExpression($oCheckIdAgainstCurrentValueExpression); + + return new DBObjectSet($oSearchForExistingCurrentValue); + } }