diff --git a/core/dbsearch.class.php b/core/dbsearch.class.php index ce37cf886..90bb38c1b 100644 --- a/core/dbsearch.class.php +++ b/core/dbsearch.class.php @@ -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 .= " .-=^=-. "; diff --git a/tests/php-unit-tests/unitary-tests/core/DBSearchCommitTest.php b/tests/php-unit-tests/unitary-tests/core/DBSearchCommitTest.php index 63baa2890..4278e8f2b 100644 --- a/tests/php-unit-tests/unitary-tests/core/DBSearchCommitTest.php +++ b/tests/php-unit-tests/unitary-tests/core/DBSearchCommitTest.php @@ -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()); + } + }