如何对“列表”查找器查询进行排序?结果未按定义的顺序显示

时间:2019-11-26 12:55:13

标签: cakephp cakephp-3.x

国家/地区未按升序显示。

$coutryTable = TableRegistry::get('Country');
$getall = $coutryTable
    ->find('list', [
        'keyField' => 'phonecode',
        'valueField' => function ($row) {
            return $row['name'] . ' (+ ' . $row['phonecode'] . ')';
        }
    ])
    ->order([
        'Country.name' => 'ASC'
    ]);

2 个答案:

答案 0 :(得分:0)

尝试分类功能https://www.w3schools.com/php/func_array_asort.asp

$coutryTable = TableRegistry::get('Country');
//pr($coutryTable);die();
$getall = $coutryTable->find('list', ['keyField' => 'phonecode', 'valueField' => function ($row) {
            return $row['name'] . ' (+ ' . $row['phonecode'] . ')';
        }])
->toArray();

asort($getAll);

print_r($getAll);

答案 1 :(得分:0)

$ coutryTable = TableRegistry :: get('Country');

    $getall = $coutryTable->find('list', ['keyField' => function ($rows) {
        return $rows['name'] . ' (+ ' . $rows['phonecode'] . ')';
    }, 'valueField' => function ($row) {
        return $row['name'] . ' (+ ' . $row['phonecode'] . ')';
    }])->order(['Country.id' => 'ASC']);