我正在开发一个 CakePHP 项目,在查询结果顺序方面给我带来了一些问题。这里使用了三个表:Buildings(关于建筑物的所有信息)、Lists 和 ListBuildings(用于构建和列表的链接表)。我没有构建应用程序,不幸的是无法更改数据库结构。我想$arrBuildings
按顺序列出建筑物ListBuildings.created
(意味着将建筑物添加到列表中的那一刻),而不是Building.name
现在看起来的顺序。
function getList($list_id) {
// Get buildings in the list
$building_ids = $this->_getBuildingsForList($list_id);
$conditions = array(
'conditions'=>array('Building.id'=>$building_ids),
'contain' => array(
'List'=>array('conditions' => array ('List.id'=>$list_id))));
// Get the list information
$this->Building->List->recursive=-1;
$List=$this->Building->List->read(null,$list_id);
$options = array(
'conditions' => array('Building.id'=>$building_ids),
'contain' => array(
'List'=>array('conditions' => array ('List.id'=>$list_id)),
'Sqft'
),
);
$this->Building->myId=$this->getUserId();
$arrBuildings = $this->Building->find('all', $options);
print_r($arrBuildings);
}
由于我使用的是 CakePHP,MVC 的模型部分似乎设置正确。使用上面的代码,print_r
显示以下数组:
Array ( [0] => Array (
[Building] => Array (
[id] => 105
[name] => Stark Tower
[address] => 123 Main St.
)
[Sqft] => Array (
[0] => Array (
[id] => 51
[list_id] => 113
[building_id] => 105
[sq_feet] => 2200.000
)
)
[List] => Array (
[0] => Array (
[id] => 113
[title] => Awesome buildings
[ListBuilding] => Array (
[id] => 95
[list_id] => 113
[building_id] => 105
[created] => 2012-10-18 09:40:00 ))))
重新声明:我如何对查询(和结果数组)进行排序 ListBuildings.Created
?
我试图匿名化代码,并且可能搞砸了一些东西,所以如果有什么没有意义,请告诉我:) 提前感谢您的任何反馈!