浏览代码

Data Exchange - Fixed issue in DBObjectSet, doubling the count of SynchroAttribute initially created

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@1108 a333f486-631f-4898-b8df-5754b55c2be0
romainq 14 年之前
父节点
当前提交
03773517bd
共有 2 个文件被更改,包括 45 次插入27 次删除
  1. 25 10
      core/dbobjectset.class.php
  2. 20 17
      synchro/synchrodatasource.class.inc.php

+ 25 - 10
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)
 		{

+ 20 - 17
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())
-					{
-						$oAttribute = new SynchroAttExtKey();
-						$oAttribute->Set('reconciliation_attcode', ''); // Blank means by pkey
-					}
-					else
+					if ($oAttDef->IsScalar() && $oAttDef->IsWritable())
 					{
-						$oAttribute = new SynchroAttribute();
+						$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);
 					}
-					$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()