Find statements in CakePHP produce an array structured as below. I've already ordered my search to produce the result set shown (ordered by combined_score). Now, I'd like to apply a sorting function on the data to sort by "average_votes". See "from this:"/"to this:" below.
在CakePHP中查找语句,生成如下所示的数组。我已经命令我的搜索生成结果集(由combined_score排序)。现在,我想在数据上应用一个排序函数,以“average_votes”排序。看“从这个:”/“到这个:”下面。
I'd really appreciate any suggestions.
我真的很感激你的建议。
From this:
Array
(
[0] => Array
(
[Vehicle] => Array
(
[id] => 52
[user_id] => 101
[name] => Ford
[total_votes] => 5
[average_votes] => 3.8
[combined_score] => 19
)
)
[1] => Array
(
[Vehicle] => Array
(
[id] => 48
[user_id] => 101
[name] => Nissan
[total_votes] => 6
[average_votes] => 5
[combined_score] => 2
)
)
)
To this:
Array
(
[0] => Array
(
[Vehicle] => Array
(
[id] => 48
[user_id] => 101
[name] => Nissan
[total_votes] => 6
[average_votes] => 5
[combined_score] => 2
)
)
[1] => Array
(
[Vehicle] => Array
(
[id] => 52
[user_id] => 101
[name] => Ford
[total_votes] => 5
[average_votes] => 3.8
[combined_score] => 19
)
)
)
2 个解决方案
#1
2
Depending on what you're actually trying to achieve you can still do this at the database level in cake, like so:
根据你实际想要达到的目标,你仍然可以在蛋糕的数据库层面上做到这一点,比如:
$this->Vehicle->find('all',array('order' => array('Vehicle.combined_score' => 'asc', 'Vehicle.average_votes' => 'desc')));
Which will first sort by combined score, and then sort by average votes
哪一种会先以总分来排序,然后以平均票数排序?
The second option is to use the cakephp Set class, like so:
第二个选项是使用cakephp设置类,比如:
$results = Set::sort($results, '{n}.Vehicle.average_votes', 'desc');
#2
2
In your controller the find function you can add option for sorting.
在您的控制器中,查找函数可以添加选项来进行排序。
$this->Vehicle->find('all',array('order' => array('Vehicle.average_votes DESC')));
#1
2
Depending on what you're actually trying to achieve you can still do this at the database level in cake, like so:
根据你实际想要达到的目标,你仍然可以在蛋糕的数据库层面上做到这一点,比如:
$this->Vehicle->find('all',array('order' => array('Vehicle.combined_score' => 'asc', 'Vehicle.average_votes' => 'desc')));
Which will first sort by combined score, and then sort by average votes
哪一种会先以总分来排序,然后以平均票数排序?
The second option is to use the cakephp Set class, like so:
第二个选项是使用cakephp设置类,比如:
$results = Set::sort($results, '{n}.Vehicle.average_votes', 'desc');
#2
2
In your controller the find function you can add option for sorting.
在您的控制器中,查找函数可以添加选项来进行排序。
$this->Vehicle->find('all',array('order' => array('Vehicle.average_votes DESC')));