From dcf872bcd5c449ab79ec98b4121d3f81ccaac3fe Mon Sep 17 00:00:00 2001 From: Pierre Goiffon Date: Mon, 13 Dec 2021 16:46:21 +0100 Subject: [PATCH] =?UTF-8?q?N=C2=B04519=20Reload=20ormLinkset=20during=20\D?= =?UTF-8?q?BObject::DBWriteLinks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This will fix a bug when doing a DBUpdate() inside \iApplicationObjectExtension::OnDBInsert : if nothing done (no Set() call for example) then \DBObject::ListChanges will return a change in all non empty ormLinkset. With this fix, no change will be returned. --- core/ormlinkset.class.inc.php | 66 +++++++++++++++++------------------ 1 file changed, 32 insertions(+), 34 deletions(-) diff --git a/core/ormlinkset.class.inc.php b/core/ormlinkset.class.inc.php index e7e9e716a..43d433c12 100644 --- a/core/ormlinkset.class.inc.php +++ b/core/ormlinkset.class.inc.php @@ -719,70 +719,68 @@ class ormLinkSet implements iDBObjectSetIterator, Iterator, SeekableIterator { unset($this->aRemoved[$sLinkKey]); } - $bIsDuplicate = true; - break; + $bIsDuplicate = true; + break; } } - if ($bIsDuplicate) - { - continue; - } + if ($bIsDuplicate) { + continue; + } } - } - else - { - if (!array_key_exists($oLink->GetKey(), $aExistingLinks)) - { + } else { + if (!array_key_exists($oLink->GetKey(), $aExistingLinks)) { $oLink->DBClone(); } } $oLink->DBWrite(); + + $this->aPreserved[$oLink->GetKey()] = $oLink; + $this->aOriginalObjects[$oLink->GetKey()] = $oLink; } - foreach ($this->aRemoved as $iLinkId) - { - if (array_key_exists($iLinkId, $aExistingLinks)) - { + $this->aAdded = []; + + foreach ($this->aRemoved as $iLinkId) { + if (array_key_exists($iLinkId, $aExistingLinks)) { $oLink = $aExistingLinks[$iLinkId]; - if ($oAttDef->IsIndirect()) - { + if ($oAttDef->IsIndirect()) { $oLink->DBDelete(); - } - else - { + } else { $oExtKeyToRemote = MetaModel::GetAttributeDef($this->sClass, $sExtKeyToMe); - if ($oExtKeyToRemote->IsNullAllowed()) - { - if ($oLink->Get($sExtKeyToMe) == $oHostObject->GetKey()) - { + if ($oExtKeyToRemote->IsNullAllowed()) { + if ($oLink->Get($sExtKeyToMe) == $oHostObject->GetKey()) { // Detach the link object from this $oLink->Set($sExtKeyToMe, 0); $oLink->DBUpdate(); } - } - else - { + } else { $oLink->DBDelete(); } } + unset($this->aPreserved[$oLink->GetKey()], $this->aOriginalObjects[$oLink->GetKey()]); } } + $this->aRemoved = []; + // Note: process modifications at the end: if a link to remove has also been listed as modified, then it will be gracefully ignored - foreach ($this->aModified as $iLinkId => $oLink) - { - if (array_key_exists($oLink->GetKey(), $aExistingLinks)) - { + foreach ($this->aModified as $iLinkId => $oLink) { + if (array_key_exists($oLink->GetKey(), $aExistingLinks)) { $oLink->DBUpdate(); - } - else - { + } else { $oLink->DBClone(); } + $this->aPreserved[$oLink->GetKey()] = $oLink; + $this->aOriginalObjects[$oLink->GetKey()] = $oLink; } + $this->aModified = []; // End of the critical section // $oMtx->Unlock(); + + // we updated the instance (original/preserved/added/modified/removed arrays) all along the way + $this->bHasDelta = false; + $this->oOriginalSet->GetFilter()->SetInternalParams(['id', $oHostObject->GetKey()]); } /**