From 8ce3ea4eac40acd233b2c57dea21b3c85a4ad12f Mon Sep 17 00:00:00 2001 From: favincen Date: Thu, 30 Oct 2025 01:33:45 +0100 Subject: [PATCH 1/3] Add helper to copy attribute only if target is null --- core/dbobject.class.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/core/dbobject.class.php b/core/dbobject.class.php index e4b8a9ff4b..46e830a12f 100644 --- a/core/dbobject.class.php +++ b/core/dbobject.class.php @@ -742,6 +742,33 @@ public function SetIfNull($sAttCode, $value) } } + /** + * Helper to copy a value only if the target is currently undefined + * + * Call Copy() only of the internal representation of the attribute is null. + * + * @api + * @see Copy() + * @see SetIfNull()) + * + * @param string $sDestAttCode + * @param string $sSourceAttCode + * + * @throws \CoreException + * @throws \CoreUnexpectedValue + * @throws \Exception + */ + public function CopyIfNull($sDestAttCode, $sSourceAttCode) + { + $oAttDef = MetaModel::GetAttributeDef(get_class($this), $sDestAttCode); + $oCurrentValue = $this->Get($sDestAttCode); + if ($oAttDef->IsNull($oCurrentValue)) + { + return $this->Copy($sDestAttCode, $sSourceAttCode); + } + return true; + } + /** * Helper to set a value that fits the attribute max size * From 1f87206da6f94098ba30473d9d7303c140116615 Mon Sep 17 00:00:00 2001 From: favincen Date: Thu, 30 Oct 2025 02:45:01 +0100 Subject: [PATCH 2/3] improve header --- core/dbobject.class.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/dbobject.class.php b/core/dbobject.class.php index 46e830a12f..efd19f92d9 100644 --- a/core/dbobject.class.php +++ b/core/dbobject.class.php @@ -745,7 +745,7 @@ public function SetIfNull($sAttCode, $value) /** * Helper to copy a value only if the target is currently undefined * - * Call Copy() only of the internal representation of the attribute is null. + * Call Copy() only of the internal representation of the target attribute is null. * * @api * @see Copy() @@ -754,6 +754,8 @@ public function SetIfNull($sAttCode, $value) * @param string $sDestAttCode * @param string $sSourceAttCode * + * @return bool true if copy was successfull or was not performed because target attribute isn't null + * * @throws \CoreException * @throws \CoreUnexpectedValue * @throws \Exception From 0c8ba7436d9b428cb6e6304f563ea80db35bfadb Mon Sep 17 00:00:00 2001 From: Fabrice VINCENT Date: Mon, 8 Dec 2025 17:27:23 +0100 Subject: [PATCH 3/3] refactoring using SetIfNull() Co-authored-by: Thomas Casteleyn --- core/dbobject.class.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/core/dbobject.class.php b/core/dbobject.class.php index efd19f92d9..75d45e7734 100644 --- a/core/dbobject.class.php +++ b/core/dbobject.class.php @@ -762,12 +762,7 @@ public function SetIfNull($sAttCode, $value) */ public function CopyIfNull($sDestAttCode, $sSourceAttCode) { - $oAttDef = MetaModel::GetAttributeDef(get_class($this), $sDestAttCode); - $oCurrentValue = $this->Get($sDestAttCode); - if ($oAttDef->IsNull($oCurrentValue)) - { - return $this->Copy($sDestAttCode, $sSourceAttCode); - } + $this->SetIfNull($sDestAttCode, $this->Get($sSourceAttCode)); return true; }