ソースを参照

#1049: CSV import (and edition) of n:n links. The Differences() function is NOT commutative: the original value (i.e. the one from the database) must the the first argument.

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@3752 a333f486-631f-4898-b8df-5754b55c2be0
dflaven 9 年 前
コミット
e10f75b167
2 ファイル変更10 行追加4 行削除
  1. 1 1
      core/dbobject.class.php
  2. 9 3
      core/dbobjectset.class.php

+ 1 - 1
core/dbobject.class.php

@@ -1270,7 +1270,7 @@ abstract class DBObject implements iDisplay
 			{
 				$oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAtt);
 				// The value is an object, the comparison is not strict
-				if (!$oAttDef->Equals($proposedValue, $this->m_aOrigValues[$sAtt]))
+				if (!$oAttDef->Equals($this->m_aOrigValues[$sAtt], $proposedValue))
 				{
 					$aDelta[$sAtt] = $proposedValue;
 					$this->m_aModifiedAtt[$sAtt] = true; // Really modified

+ 9 - 3
core/dbobjectset.class.php

@@ -1140,10 +1140,13 @@ function HashCountComparison($a, $b) // Sort descending on 'count'
 
 /**
  * Helper class to compare the content of two DBObjectSets based on the fingerprints of the contained objects
- * When computing the actual differences, the algorithm tries to preserve as much as possible the existing
+ * The FIRST SET MUST BE LOADED FROM THE DATABASE, the second one can be a set of objects in memory
+ * When computing the actual differences, the algorithm tries to preserve as much as possible the EXISTING
  * objects (i.e. prefers 'modified' to 'removed' + 'added')
  * 
- * LIMITATION: only DBObjectSets with one column (i.e. one class of object selected) are supported
+ * LIMITATIONS:
+ *  - only DBObjectSets with one column (i.e. one class of object selected) are supported
+ *  - the first set must be the one loaded from the database
  */
 class DBObjectSetComparator
 {
@@ -1275,7 +1278,8 @@ class DBObjectSetComparator
 	}
 	
 	/**
-	 * Get the list of differences between the two sets.
+	 * Get the list of differences between the two sets. In ordeer to write back into the database only the minimum changes
+	 * THE FIRST SET MUST BE THE ONE LOADED FROM THE DATABASE
 	 * Returns a hash: 'added' => DBObject(s), 'removed' => DBObject(s), 'modified' => DBObjects(s)
 	 * @return Ambigous <int:DBObject: , unknown>
 	 */
@@ -1287,6 +1291,8 @@ class DBObjectSetComparator
 		// Check that all objects in Set1 are also in Set2
 		foreach($this->aFingerprints1 as $sFingerprint => $oObj)
 		{
+			// Beware: the elements from the first set MUST come from the database, otherwise the result will be irrelevant
+			if ($oObj->IsNew()) throw new Exception('Cannot compute differences when elements from the first set are NOT in the database');
 			if (array_key_exists($oObj->GetKey(), $this->aIDs2) && ($this->aIDs2[$oObj->GetKey()]->IsModified()))
 			{
 				// The very same object exists in both set, but was modified since its load