我试图弄清楚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显示记录!?
我该如何解决?
答案 0 :(得分: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