1

CakePHP 中的 Find 语句生成一个结构如下的数组。我已经命令我的搜索生成显示的结果集(按 combine_score 排序)。现在,我想对数据应用排序函数以按“average_votes”排序。请参阅下面的“从这个:”/“到这个:”。

我真的很感激任何建议。

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
            )

    )

)
4

2 回答 2

2

在您的控制器中,您可以添加查找功能以进行排序。

$this->Vehicle->find('all',array('order' => array('Vehicle.average_votes DESC')));
于 2012-05-02T10:02:02.357 回答
2

根据您实际尝试实现的目标,您仍然可以在蛋糕的数据库级别执行此操作,如下所示:

$this->Vehicle->find('all',array('order' => array('Vehicle.combined_score' => 'asc', 'Vehicle.average_votes' => 'desc')));

哪个将首先按综合得分排序,然后按平均票数排序

第二个选项是使用 cakephp Set 类,如下所示:

$results = Set::sort($results, '{n}.Vehicle.average_votes', 'desc');
于 2012-05-02T10:28:47.943 回答