duanjiao5723 2017-10-24 08:49
浏览 30
已采纳

显示名称而不是ID - YII2

In my form I have the category_id, In the drop-down, it displays the name but after saving it displays the ID, how do I display the name instead of the 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 );
          });'
     ]); 
?>

What I have tried so far in the view is this:

[
   'label' => $model->category_id->getAttributeLabel('category_name'),
   'value' => $model->category_id->category_name
]

But I'm getting an error: Call to a member function getAttributeLabel() on integer

  • 写回答

2条回答 默认 最新

  • dopii22884 2017-10-24 09:56
    关注

    please try this code in your grid-view.

    ['label'=>'Category Name',
    'value' => function ($data) {
         return Category::findOne(['id'=>$data->category_id])->category_name;
    },]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 halcon ocr mlp 识别问题
  • ¥15 已知曲线满足正余弦函数,根据其峰值,还原出整条曲线
  • ¥20 无法创建新的堆栈防护界面
  • ¥15 sessionStorage在vue中的用法
  • ¥15 wordpress更换域名后用户图片头像不显示
  • ¥15 如何在ubunto上安装CEF (Chromium Embedded Framework),并且基于qt实现打开一个web
  • ¥30 AD9854 为什么输出波形幅度受限,AI机器人勿扰
  • ¥15 如何在ubunto上安装CEF (Chromium Embedded Framework
  • ¥15 如何联系真正的开发者而非公司
  • ¥15 有偿求苍穹外卖环境配置