Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 显示名称而不是ID-YII2_Php_Mysql_Yii2_Yii2 Advanced App - Fatal编程技术网

Php 显示名称而不是ID-YII2

Php 显示名称而不是ID-YII2,php,mysql,yii2,yii2-advanced-app,Php,Mysql,Yii2,Yii2 Advanced App,在我的表单中,我有一个category_id,在下拉列表中,它显示名称,但在保存后显示id,如何显示名称而不是id <?= $form->field($model, 'category_id')->dropDownlist( ArrayHelper::map(Category::find()->all(), 'id', 'category_name'), [ 'prompt' => 'Select Category',

在我的表单中,我有一个category_id,在下拉列表中,它显示名称,但在保存后显示id,如何显示名称而不是id

<?= $form->field($model, 'category_id')->dropDownlist(
     ArrayHelper::map(Category::find()->all(), 'id', 'category_name'),
     [
         'prompt' => 'Select Category',
         'onchange' => '$.post( "index.php?r=sub-cat/lists&id='.'"+$(this).val(), function( data ) {
                $( "select#tickets-sub_category" ).html( data );
          });'
     ]); 
?>

但是我得到了一个错误:在整数上调用成员函数getAttributeLabel()

请在网格视图中尝试此代码

['label'=>'Category Name',
'value' => function ($data) {
     return Category::findOne(['id'=>$data->category_id])->category_name;
},]

如果要在视图的任何位置显示类别名称而不是id,请尝试此代码 假设你有两张桌子 1.Catgeory(身份证、姓名) 2.职位(id、类别、职务) 在你的帖子模型中

public function getCategoryId()
    {
        return $this->hasOne(\path\to\models\Category::className(), ['id' 
        => 'category']);
    }
/**
 * @getCategoryName
 *
 */
public function getCategoryName()
{
    return $this->categoryId ? $this->categoryId->name : '- no category -';
}
现在您可以在视图中的任何位置使用categoryName?>了

如果您使用的是GridView
只需将属性更改为categoryName

这是在类别模型上还是在view.php上?请在网格视图中使用它。在view.php中如何?我会做类似的事情吗?编辑:我尝试过使用它,但它说array#u merge():Argument#2不是一个数组。您是否使用网格视图?在your view.php中?不,是详细视图。
public function getCategoryId()
    {
        return $this->hasOne(\path\to\models\Category::className(), ['id' 
        => 'category']);
    }
/**
 * @getCategoryName
 *
 */
public function getCategoryName()
{
    return $this->categoryId ? $this->categoryId->name : '- no category -';
}