php - 在 Yii 框架中,如何组合列并在下拉列表中显示为显示字符串

标签 php yii html.dropdownlistfor virtual-attribute yii-cmodel

在我看来,我有一个 dropDownList,它是从 clients 表中填充的,该表包含像 first_namelast_name 这样的列id 等,现在我想显示 first_namelast_name 作为显示文本和 id 作为下拉列表中的值,我已完成使用 id 作为值和 first_name 作为显示文本,但在这里我想合并这些列 (first_namelast_name) 并用作显示文本。

在模型中

function getClients()
{
    $Clients = Client::model()->findAll();
    $list    = CHtml::listData($Clients , 'client_id', 'first_name');
    return $list;
}

可见

echo $form->dropDownList($model,'client_id',$model->getClients());

最佳答案

还有一个方法 在模型中

function getFullName()
{
    return $this->first_name.' '.$this->last_name;
}

function getClients()
{
    $Clients = Client::model()->findAll();
    $list    = CHtml::listData($Clients , 'client_id', 'fullName');
    return $list;
}

我认为这是一种可重用的方法,因为您不仅可以在下拉列表中使用 fullName 虚拟属性,而且可以在任何需要全名的地方使用。

关于php - 在 Yii 框架中,如何组合列并在下拉列表中显示为显示字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12812062/

相关文章:

PHP "cannot access protected property"

javascript - 如何将此多点表单制作成数组

php - FPDF 错误 : Some data has already been output, 无法在 000webhost 上发送 PDF 文件

php - 如何在 Yii 中用数据库值填充文本字段?

php - 如何在 CDbCriteria Yii Framework 中编写复杂的 JOIN 查询

yii:如何为两个属性制定唯一规则

asp.net-mvc - mvc c# html.dropdownlist 和 viewbag

c# - 在 Html.DropDownlistFor 中获取多个选定值

php - 如何创建主键是字符?

php - 如何使用下拉选项过滤数据