Ver código fonte

#983 Sortering not possible on multi-column queries

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@3381 a333f486-631f-4898-b8df-5754b55c2be0
romainq 10 anos atrás
pai
commit
a97ec14388

+ 3 - 0
core/dbobjectsearch.class.php

@@ -972,6 +972,9 @@ class DBObjectSearch
 
 	// Alternative to object mapping: the data are transfered directly into an array
 	// This is 10 times faster than creating a set of objects, and makes sense when optimization is required
+	/**
+	 * @param hash $aOrderBy Array of '[<classalias>.]attcode' => bAscending
+	 */	
 	public function ToDataArray($aColumns = array(), $aOrderBy = array(), $aArgs = array())
 	{
 		$sSQL = MetaModel::MakeSelectQuery($this, $aOrderBy, $aArgs);

+ 1 - 3
core/dbobjectset.class.php

@@ -48,7 +48,7 @@ class DBObjectSet
 	 * Create a new set based on a Search definition.
 	 * 
 	 * @param DBObjectSearch $oFilter The search filter defining the objects which are part of the set (multiple columns/objects per row are supported)
-	 * @param hash $aOrderBy
+	 * @param hash $aOrderBy Array of '[<classalias>.]attcode' => bAscending
 	 * @param hash $aArgs Values to substitute for the search/query parameters (if any). Format: param_name => value
 	 * @param hash $aExtendedDataSpec
 	 * @param int $iLimitCount Maximum number of rows to load (i.e. equivalent to MySQL's LIMIT start, count)
@@ -1156,5 +1156,3 @@ function HashCountComparison($a, $b) // Sort descending on 'count'
     }
     return ($a['count'] > $b['count']) ? -1 : 1;
 }
-
-?>

+ 27 - 14
core/metamodel.class.php

@@ -1,5 +1,5 @@
 <?php
-// Copyright (C) 2010-2013 Combodo SARL
+// Copyright (C) 2010-2014 Combodo SARL
 //
 //   This file is part of iTop.
 //
@@ -2218,6 +2218,9 @@ abstract class MetaModel
 	}
 
 
+	/**
+	 * @param hash $aOrderBy Array of '[<classalias>.]attcode' => bAscending
+	 */	
 	public static function MakeSelectQuery(DBObjectSearch $oFilter, $aOrderBy = array(), $aArgs = array(), $aAttToLoad = null, $aExtendedDataSpec = null, $iLimitCount = 0, $iLimitStart = 0, $bGetCount = false)
 	{
 		// Check the order by specification, and prefix with the class alias
@@ -2228,32 +2231,44 @@ abstract class MetaModel
 		$aOrderSpec = array();
 		foreach ($aOrderBy as $sFieldAlias => $bAscending)
 		{
-			if ($sFieldAlias != 'id')
-			{
-				MyHelpers::CheckValueInArray('field name in ORDER BY spec', $sFieldAlias, self::GetAttributesList($sClass));
-			}
 			if (!is_bool($bAscending))
 			{
 				throw new CoreException("Wrong direction in ORDER BY spec, found '$bAscending' and expecting a boolean value");
 			}
-			
-			if (self::IsValidAttCode($sClass, $sFieldAlias))
+
+			$iDotPos = strpos($sFieldAlias, '.');
+			if ($iDotPos === false)
 			{
-				$oAttDef = self::GetAttributeDef($sClass, $sFieldAlias);
-				foreach($oAttDef->GetOrderBySQLExpressions($sClassAlias) as $sSQLExpression)
+				$sAttClass = $sClass;
+				$sAttClassAlias = $sClassAlias;
+				$sAttCode = $sFieldAlias;
+			}
+			else
+			{
+				$sAttClassAlias = substr($sFieldAlias, 0, $iDotPos);
+				$sAttClass = $oFilter->GetClassName($sAttClassAlias);
+				$sAttCode = substr($sFieldAlias, $iDotPos + 1);
+			}
+
+			if ($sAttCode != 'id')
+			{
+				MyHelpers::CheckValueInArray('field name in ORDER BY spec', $sAttCode, self::GetAttributesList($sAttClass));
+
+				$oAttDef = self::GetAttributeDef($sAttClass, $sAttCode);
+				foreach($oAttDef->GetOrderBySQLExpressions($sAttClassAlias) as $sSQLExpression)
 				{
 					$aOrderSpec[$sSQLExpression] = $bAscending;
 				}
 			}
 			else
 			{
-				$aOrderSpec['`'.$sClassAlias.$sFieldAlias.'`'] = $bAscending;
+				$aOrderSpec['`'.$sAttClassAlias.$sAttCode.'`'] = $bAscending;
 			}
 
 			// Make sure that the columns used for sorting are present in the loaded columns
-			if (!is_null($aAttToLoad) && !isset($aAttToLoad[$sClassAlias][$sFieldAlias]))
+			if (!is_null($aAttToLoad) && !isset($aAttToLoad[$sAttClassAlias][$sAttCode]))
 			{
-				$aAttToLoad[$sClassAlias][$sFieldAlias] = MetaModel::GetAttributeDef($sClass, $sFieldAlias);
+				$aAttToLoad[$sAttClassAlias][$sAttCode] = MetaModel::GetAttributeDef($sAttClass, $sAttCode);
 			}			
 		}
 
@@ -5509,5 +5524,3 @@ MetaModel::RegisterZList("preview", array("description"=>"All attributes visible
 
 MetaModel::RegisterZList("standard_search", array("description"=>"List of criteria for the standard search", "type"=>"filters"));
 MetaModel::RegisterZList("advanced_search", array("description"=>"List of criteria for the advanced search", "type"=>"filters"));
-
-?>

+ 3 - 2
core/valuesetdef.class.inc.php

@@ -100,6 +100,9 @@ class ValueSetObjects extends ValueSetDefinition
 	private $m_bAllowAllData;
 	private $m_aModifierProperties;
 
+	/**
+	 * @param hash $aOrderBy Array of '[<classalias>.]attcode' => bAscending
+	 */	
 	public function __construct($sFilterExp, $sValueAttCode = '', $aOrderBy = array(), $bAllowAllData = false, $aModifierProperties = array())
 	{
 		$this->m_sContains = '';
@@ -415,5 +418,3 @@ class ValueSetEnumClasses extends ValueSetEnum
 		return true;
 	}
 }
-
-?>

+ 5 - 5
pages/ajax.render.php

@@ -1,5 +1,5 @@
 <?php
-// Copyright (C) 2010-2013 Combodo SARL
+// Copyright (C) 2010-2014 Combodo SARL
 //
 //   This file is part of iTop.
 //
@@ -120,16 +120,16 @@ try
 								if ($aNameSpec[0] == '%1$s')
 								{
 									// The name is made of a single column, let's sort according to the sort algorithm for this column
-									$aOrderBy[$aNameSpec[1][0]] = (utils::ReadParam('sort_order', 'asc') == 'asc');
+									$aOrderBy[$sAlias.'.'.$aNameSpec[1][0]] = (utils::ReadParam('sort_order', 'asc') == 'asc');
 								}
 								else
 								{
-									$aOrderBy['friendlyname'] = (utils::ReadParam('sort_order', 'asc') == 'asc');
+									$aOrderBy[$sAlias.'.'.'friendlyname'] = (utils::ReadParam('sort_order', 'asc') == 'asc');
 								}
 							}
 							else
 							{
-								$aOrderBy['friendlyname'] = (utils::ReadParam('sort_order', 'asc') == 'asc');
+								$aOrderBy[$sAlias.'.'.'friendlyname'] = (utils::ReadParam('sort_order', 'asc') == 'asc');
 							}
 						}
 					}
@@ -155,7 +155,7 @@ try
 							{
 								$sSortCol = $sAttCode;
 							}
-							$aOrderBy[$sSortCol] = (utils::ReadParam('sort_order', 'asc') == 'asc');
+							$aOrderBy[$sAlias.'.'.$sSortCol] = (utils::ReadParam('sort_order', 'asc') == 'asc');
 						}
 					}
 					$iSortIndex++;