2014-03-25 73 views
-1

我試圖弄清楚什麼是錯上xPDO SQL查詢排序順序,對象的排序順序是錯誤

這個查詢

$criteria = $this->modx->newQuery($table); 
$criteria->sortby($sortby,$sortdir); 
$options = $this->modx->getCollectionGraph($table,$criteria); 
$criteria->prepare(); 
echo '<pre>'.$criteria->toSQL().'</pre>'; 

結果在這個SQL〜這是正確的:

SELECT `Location`.`id` AS `Location_id`, `Location`.`created` AS `Location_created`, 
`Location`.`modified` AS `Location_modified`, `Location`.`location` AS 
`Location_location`, `Location`.`group` AS `Location_group`, `Location`.`comment` 
AS `Location_comment` 
FROM `flow_location` AS `Location` ORDER BY location asc 

但是,如果我嘗試遍歷查詢:

foreach($options as $option) { 
    echo $option->get($value).'<br>'; 
} 

它會通過數據庫中的訂單ID顯示記錄!?

我該如何解決這個問題?

回答

1

您正在訂購location asc,但location未在您的查詢中定義。您將列location重新命名爲Location_location,因此您應該訂購Location_location asc

SELECT `Location`.`id` AS `Location_id`, `Location`.`created` AS `Location_created`, 
`Location`.`modified` AS `Location_modified`, `Location`.`location` AS 
`Location_location`, `Location`.`group` AS `Location_group`, `Location`.`comment` 
AS `Location_comment` 
FROM `flow_location` AS `Location` ORDER BY location asc 
+0

如果我在一個SQL瀏覽器中運行該查詢它的工作和爲了正確 - 我看到你說的話試圖通過Location_location訂購yeilds相同的結果[$基準 - > sortby($表.'_」 。$ sortby,$ sortdir);我不確定你可以對getCollectionGraph進行排序,因爲如果我只是使用'getCollection' –

+0

Gotcha,代碼就可以工作。不太瞭解modx,但是你可以在php中用usort()函數查詢後排序對象。舉一些例子來說吧 –