Sugarcrm 下拉值排序

Sugarcrm dropdown value sorting

这是我的代码:

custom/Extension/application/Ext/Utils/ or custom/include/custom_utils.php

<?php

function getActiveReleases()
{
    $query = "SELECT id, name FROM releases where deleted=0 and status='Active' order by list_order asc";
    $result = $GLOBALS['db']->query($query, false);

    $list = array();
    $list['']='';
    while (($row = $GLOBALS['db']->fetchByAssoc($result)) != null) {
        $list[$row['id']] = $row['name'];
    }

    return $list;
}

unset($dictionary['MODULENAME']['fields']['FIELDNAME']['options']);
$dictionary['MODULENAME']['fields']['FIELDNAME']['function'] = 'getActiveReleases';

Refrence From here

我在我的代码中做了同样的事情,它工作正常,除了 order by : order by list_order asc .
Drop down should come顺序就像:order by list_order asc .
但是 sugar 覆盖它并按下拉列表的键值对下拉列表进行排序:id
我想要键值作为 ID 但排序应该是这样的:order by list_order asc

我在 Google 上搜索,但我没有找到任何方法,所以我发布了这个问题。

替换为:

$list[$row['id']] = $row['name'];

按索引数组:

$arrayIndex = 0;
while (($row = $GLOBALS['db']->fetchByAssoc($result)) != null) {
    $list[$arrayIndex] = $row['name'];
    $arrayIndex++;
}

这不是关于 sugarcms ,而是关于 php 本身

当您提供 $row['id'] 作为数组键时,它是按自然排序的,

php会按照这个变量值的顺序取值

举个简单的例子:https://3v4l.org/8HjFB,我希望这能让你更清楚

在你的 while 语句之后怎么样?

asort($list);
return $list;

来自documentation

This function sorts an array such that array indices maintain their correlation with the array elements they are associated with. This is used mainly when sorting associative arrays where the actual element order is significant.