N°8511 - Enhance DBObjectSet to be able to order by id

This commit is contained in:
odain
2025-08-18 14:37:48 +02:00
parent e6a0d95cba
commit d80e26791d
2 changed files with 33 additions and 13 deletions

View File

@@ -372,7 +372,7 @@ abstract class DBSearch
* @internal
*
* @param string $sAttSpec Can be either an attribute code or extkey->[sAttSpec] or linkset->[sAttSpec] and so on, recursively
* Example: infra_list->ci_id->location_id->country
* Example: infra_list->ci_id->location_id->country
* @param mixed $value The value to match (can be an array => IN(val1, val2...)
* @return void
*/
@@ -769,7 +769,7 @@ abstract class DBSearch
$oKPI = new ExecutionKPI();
$result = apc_fetch($sAPCCacheId);
$oKPI->ComputeStats('Search APC (fetch)', $sQuery);
if (is_object($result))
{
$oResultFilter = $result;
@@ -785,17 +785,17 @@ abstract class DBSearch
$oOql = new OqlInterpreter($sQuery);
$oOqlQuery = $oOql->ParseQuery();
if ($oMetaModel === null)
{
$oMetaModel = new ModelReflectionRuntime();
}
$oOqlQuery->Check($oMetaModel, $sQuery); // Exceptions thrown in case of issue
$oResultFilter = $oOqlQuery->ToDBSearch($sQuery);
$oKPI->ComputeStats('Parse OQL', $sQuery);
if ($bOQLCacheEnabled)
{
self::$m_aOQLQueries[$sQueryId] = $oResultFilter->DeepClone();
@@ -1087,17 +1087,17 @@ abstract class DBSearch
{
$aOrderSpec[$sSQLExpression] = $bAscending;
}
}
else
{
$aOrderSpec['`'.$sAttClassAlias.$sAttCode.'`'] = $bAscending;
}
// Make sure that the columns used for sorting are present in the loaded columns
if (!is_null($aAttToLoad) && !isset($aAttToLoad[$sAttClassAlias][$sAttCode]))
{
$aAttToLoad[$sAttClassAlias][$sAttCode] = MetaModel::GetAttributeDef($sAttClass, $sAttCode);
}
}
}
else
{
$aOrderSpec['`'.$sAttClassAlias.$sAttCode.'`'] = $bAscending;
}
}
$oSQLQuery = $this->GetSQLQuery($aOrderBy, $aArgs, $aAttToLoad, $aExtendedDataSpec, $iLimitCount, $iLimitStart, $bGetCount);
@@ -1255,7 +1255,7 @@ abstract class DBSearch
$oSQLQueryExt = new SQLObjectQuery($aExtendedDataSpec['table'], $sTableAlias, $aExtendedFields);
$oSQLQuery->AddInnerJoin($oSQLQueryExt, 'id', $aExtendedDataSpec['join_key'] /*, $sTableAlias*/);
}
return $oSQLQuery;
}
@@ -1590,7 +1590,7 @@ abstract class DBSearch
}
$aBacktrace = debug_backtrace();
$iCallStackPos = count($aBacktrace) - self::$m_bDebugQuery;
$sIndent = "";
$sIndent = "";
for ($i = 0 ; $i < $iCallStackPos ; $i++)
{
$sIndent .= " .-=^=-. ";

View File

@@ -88,4 +88,24 @@ class DBSearchCommitTest extends ItopDataTestCase
static::assertEquals(0, $oSet->Count());
}
/**
* @covers N°8511 - Enhance DBObjectSet to be able to order by id
*/
public function testDbObjectSetFetchMethodWorksWithOptimizeColumnLoadOrderedById(){
$sUID=uniqid();
$oOrg1 = $this->CreateOrganization($sUID);
$oOrg2 = $this->CreateOrganization($sUID);
$oSearch = DBSearch::FromOQL("SELECT Organization WHERE name=\"$sUID\"", ['uuid' => $sUID]);
$oSet = new \DBObjectSet($oSearch, ['name' => true, 'id' => false ]);
$oSet->OptimizeColumnLoad(['Organization' => ['name']]);
static::assertEquals(2, $oSet->Count());
static::assertEquals($oOrg2->GetKey(), $oSet->Fetch()->GetKey());
$oSet = new \DBObjectSet($oSearch, ['name' => true, 'id' => true ]);
static::assertEquals(2, $oSet->Count());
static::assertEquals($oOrg1->GetKey(), $oSet->Fetch()->GetKey());
}
}