Browse Source

Fixed a regression in the deletion (and simplified the algorithm, though more queries will be issued)

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@1142 a333f486-631f-4898-b8df-5754b55c2be0
romainq 14 years ago
parent
commit
cc4481ec3e
2 changed files with 29 additions and 5 deletions
  1. 5 0
      core/cmdbsource.class.inc.php
  2. 24 5
      core/dbobject.class.php

+ 5 - 0
core/cmdbsource.class.inc.php

@@ -243,6 +243,11 @@ class CMDBSource
 		return false;
 	}
 
+	public static function DeleteFrom($sSQLQuery)
+	{
+		self::Query($sSQLQuery);
+	}
+
 	public static function QueryToScalar($sSql)
 	{
 		$result = mysql_query($sSql, self::$m_resDBLink);

+ 24 - 5
core/dbobject.class.php

@@ -1183,18 +1183,37 @@ abstract class DBObject
 		}
 	}
 
+	private function DBDeleteSingleTable($sTableClass)
+	{
+		$sTable = MetaModel::DBGetTable($sTableClass);
+		// Abstract classes or classes having no specific attribute do not have an associated table
+		if ($sTable == '') return;
+
+		$sPKField = '`'.MetaModel::DBGetKey($sTableClass).'`';
+		$sKey = CMDBSource::Quote($this->m_iKey);
+
+		$sDeleteSQL = "DELETE FROM `$sTable` WHERE $sPKField = $sKey";
+		CMDBSource::DeleteFrom($sDeleteSQL);
+	}
+
+	private function DBDeleteInternal()
+	{
+		$sClass = get_class($this);
+
+		foreach(MetaModel::EnumParentClasses($sClass, ENUM_PARENT_CLASSES_ALL) as $sParentClass)
+		{
+			$this->DBDeleteSingleTable($sParentClass);
+		}
+	}
+	
 	// Delete a record
 	public function DBDelete()
 	{
-		$oFilter = new DBObjectSearch(get_class($this));
-		$oFilter->AddCondition('id', $this->m_iKey, '=');
-
 		$this->OnDelete();
 
-		$sSQL = MetaModel::MakeDeleteQuery($oFilter);
 		if (!MetaModel::DBIsReadOnly())
 		{
-			CMDBSource::Query($sSQL);
+			$this->DBDeleteInternal();
 		}
 
 		$this->AfterDelete();