2 Replies - 263 Views - Last Post: 24 October 2017 - 03:42 AM Rate Topic: -----

#1 sadboys  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 10-August 15

Displaying the name instead of the ID

Posted 24 October 2017 - 02:58 AM

In my dropdown I have list of categories, after choosing a category in the view it displays the category_id instead of the name.
How do I display the category name in the view instead of the ID?

What I have tried so far I edited the code in view to this:

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



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

Is This A Good Question/Topic? 0
  • +

Replies To: Displaying the name instead of the ID

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4136
  • View blog
  • Posts: 13,058
  • Joined: 08-June 10

Re: Displaying the name instead of the ID

Posted 24 October 2017 - 03:00 AM

How does your View process the data?
Was This Post Helpful? 0
  • +
  • -

#3 sadboys  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 10-August 15

Re: Displaying the name instead of the ID

Posted 24 October 2017 - 03:42 AM

View PostDormilich, on 24 October 2017 - 03:00 AM, said:

How does your View process the data?


Yii2 comes already with the DetailView after generating CRUD. DetailView uses the $attributes property to determines which model attributes should be displayed and how they should be formatted.


<?php

use yii\helpers\Html;
use yii\widgets\DetailView;

/* @var $this yii\web\View */
/* @var $model app\models\Tickets */

$this->title = $model->id;
$this->params['breadcrumbs'][] = ['label' => 'Tickets', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="tickets-view">

    <h1><?= Html::encode($this->title) ?></h1>

    <p>
        <?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
        <?= Html::a('Delete', ['delete', 'id' => $model->id], [
            'class' => 'btn btn-danger',
            'data' => [
                'confirm' => 'Are you sure you want to delete this item?',
                'method' => 'post',
            ],
        ]) ?>
    </p>

    <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            'id',
            'request_title',
            'status',
            'time_start',
            'time_end',
            'details',
        //  'category',
            'sub_category',
            'room_no',
            'employee_id',
            [
                'value' => function ($model) {
                    return Category::findOne(['id' => $data->category_id])->category_name;
                }
            ],
        //  'category_id',
        ],
    ]) ?>

</div>



Nevermind, I already figured it out.

By changing

 <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            'id',
            'request_title',
            'status',
            'time_start',
            'time_end',
            'details',
        //  'category',
            'sub_category',
            'room_no',
            'employee_id',
            'category_id,
           
    ]) ?>


Into this:

[code] <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            'id',
            'request_title',
            'status',
            'time_start',
            'time_end',
            'details',
        //  'category',
            'sub_category',
            'room_no',
            'employee_id',
            ['label'=>'Category Name',
                'value' => function ($data) {
                     return Category::findOne(['id'=>$data->category_id])->category_name;
                },
                ]
        //  'category_id',
        ],
    ]) ?>
[/code]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1