diff --git a/core/dbobjectset.class.php b/core/dbobjectset.class.php index 418a7955f..1255fa79e 100644 --- a/core/dbobjectset.class.php +++ b/core/dbobjectset.class.php @@ -47,7 +47,7 @@ class DBObjectSet $this->m_iLimitCount = $iLimitCount; $this->m_iLimitStart = $iLimitStart; - $this->m_bLoaded = false; + $this->m_bLoaded = false; // true when the filter has been used OR the set is built step by step (AddObject...) $this->m_aData = array(); // array of (row => array of (classalias) => object) $this->m_aId2Row = array(); $this->m_iCurrRow = 0; @@ -203,6 +203,7 @@ class DBObjectSet public function GetFilter() { + // #@# This is false as soon as the set has been manipulated (AddObject...) return $this->m_oFilter; } @@ -240,6 +241,9 @@ class DBObjectSet public function Load() { if ($this->m_bLoaded) return; + // Note: it is mandatory to set this value now, to protect against reentrance + $this->m_bLoaded = true; + if ($this->m_iLimitCount > 0) { $sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs, $this->m_aExtendedDataSpec, $this->m_iLimitCount, $this->m_iLimitStart); @@ -263,19 +267,24 @@ class DBObjectSet $this->AddObjectExtended($aObjects); } CMDBSource::FreeResult($resQuery); - - $this->m_bLoaded = true; } public function Count() { - $sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs, null, 0, 0, true); - $resQuery = CMDBSource::Query($sSQL); - if (!$resQuery) return 0; - - $aRow = CMDBSource::FetchArray($resQuery); - CMDBSource::FreeResult($resQuery); - return $aRow['COUNT']; + if ($this->m_bLoaded && ($this->m_iLimitCount == 0) && ($this->m_iLimitStart == 0)) + { + return count($this->m_aData); + } + else + { + $sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs, null, 0, 0, true); + $resQuery = CMDBSource::Query($sSQL); + if (!$resQuery) return 0; + + $aRow = CMDBSource::FetchArray($resQuery); + CMDBSource::FreeResult($resQuery); + return $aRow['COUNT']; + } } public function Fetch($sClassAlias = '') @@ -327,6 +336,8 @@ class DBObjectSet public function AddObject($oObject, $sClassAlias = '') { + if (!$this->m_bLoaded) $this->Load(); + if (strlen($sClassAlias) == 0) { $sClassAlias = $this->m_oFilter->GetClassAlias(); @@ -339,6 +350,8 @@ class DBObjectSet protected function AddObjectExtended($aObjectArray) { + if (!$this->m_bLoaded) $this->Load(); + $iNextPos = count($this->m_aData); foreach ($aObjectArray as $sClassAlias => $oObject) @@ -350,6 +363,8 @@ class DBObjectSet public function AddObjectArray($aObjects, $sClassAlias = '') { + if (!$this->m_bLoaded) $this->Load(); + // #@# todo - add a check on the object class ? foreach ($aObjects as $oObj) { diff --git a/synchro/synchrodatasource.class.inc.php b/synchro/synchrodatasource.class.inc.php index 91d521a5e..f07839749 100644 --- a/synchro/synchrodatasource.class.inc.php +++ b/synchro/synchrodatasource.class.inc.php @@ -487,29 +487,32 @@ EOF // for each field of the target class // Create all the SynchroAttribute records $oAttributeSet = $this->Get('attribute_list'); - foreach(MetaModel::ListAttributeDefs($this->GetTargetClass()) as $sAttCode=>$oAttDef) + if ($oAttributeSet->Count() == 0) { - if ($oAttDef->IsScalar() && $oAttDef->IsWritable()) + foreach(MetaModel::ListAttributeDefs($this->GetTargetClass()) as $sAttCode=>$oAttDef) { - $oAttDef = MetaModel::GetAttributeDef($this->GetTargetClass(), $sAttCode); - if ($oAttDef->IsExternalKey()) + if ($oAttDef->IsScalar() && $oAttDef->IsWritable()) { - $oAttribute = new SynchroAttExtKey(); - $oAttribute->Set('reconciliation_attcode', ''); // Blank means by pkey + $oAttDef = MetaModel::GetAttributeDef($this->GetTargetClass(), $sAttCode); + if ($oAttDef->IsExternalKey()) + { + $oAttribute = new SynchroAttExtKey(); + $oAttribute->Set('reconciliation_attcode', ''); // Blank means by pkey + } + else + { + $oAttribute = new SynchroAttribute(); + } + $oAttribute->Set('sync_source_id', $this->GetKey()); + $oAttribute->Set('attcode', $sAttCode); + $oAttribute->Set('reconcile', MetaModel::IsReconcKey($this->GetTargetClass(), $sAttCode) ? 1 : 0); + $oAttribute->Set('update', 1); + $oAttribute->Set('update_policy', 'master_locked'); + $oAttributeSet->AddObject($oAttribute); } - else - { - $oAttribute = new SynchroAttribute(); - } - $oAttribute->Set('sync_source_id', $this->GetKey()); - $oAttribute->Set('attcode', $sAttCode); - $oAttribute->Set('reconcile', MetaModel::IsReconcKey($this->GetTargetClass(), $sAttCode) ? 1 : 0); - $oAttribute->Set('update', 1); - $oAttribute->Set('update_policy', 'master_locked'); - $oAttributeSet->AddObject($oAttribute); } + $this->Set('attribute_list', $oAttributeSet); } - $this->Set('attribute_list', $oAttributeSet); } } public function DoCheckToWrite()