beyond
beyond

Reputation: 81

Getting unknown property in Yii

under the course content there, I want to make the Active column to show either "Yes" or **"No"**At first it was showing "1" or "0", but that's not what I want to show. These are my attempted codes, and I'm getting this error. Appreciate it if anyone can please guide me.

Unknown Property – yii\base\UnknownPropertyException Getting unknown property: app\models\Coursecontent::isActive

enter image description here

<?php

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

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

$this->title = $model->course_name;
$this->params['breadcrumbs'][] = ['label' => 'Courses', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\yii\web\YiiAsset::register($this);
?>
<div class="course-view">

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

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

    <?= DetailView::widget([
        'model' => $model,
        'attributes' => [     
            'course_name',
            'description',
            ['attribute' => 'active', 'value' => $model->isActive,'contentOptions' => ['style' => $model->active == 1 ? 'color:green' :'color:red']],
            'lastupdate',
        ],
    ]) ?>

<h2>Course Content</h2>

<?= Html::a('Create Coursecontent', ['coursecontent/create'], ['class' => 'btn btn-success']) ?>


<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'chapter_name',
        'description',
        'video_link',
        ['attribute' => 'active',
             'value' => function ($model, $key, $index, $column) {return $model->isActive;},
             'contentOptions' => function ($model, $key, $index, $column) { 
                return $model->active == 1 ? ['style' => 'color:green'] : ['style' => 'color:red']; 
            }],

        ['class' => 'yii\grid\ActionColumn',
             
             'contentOptions' => ['class' => 'text-center'],
             'buttons' => [
                'view' => function ($url,$model) {
                    return Html::a('<span class="glyphicon glyphicon-eye-open"></span>',['coursecontent/view', 'id' => $model->coursecontent_id]);
                },
                'update' => function ($url,$model) {
                    return Html::a('<span class="glyphicon glyphicon-pencil"></span>',['coursecontent/update', 'id' => $model->coursecontent_id]);
                },
                'delete' => function ($url,$model) {
                    return Html::a('<span class="glyphicon glyphicon-trash"></span>',['coursecontent/delete', 'id' => $model->coursecontent_id],
                        ['data' => [
                            'confirm' => 'Are you sure you want to delete this item?',
                            'method' => 'post',
                        ],
                    ]);
                },
            ]],
    ],
]); ?>
</div>

Upvotes: 0

Views: 1866

Answers (1)

bpanatta
bpanatta

Reputation: 499

Have to make 2 changes to your code.

  1. Change all $model->isActive to $model->active.
  2. Return "Yes" or "No" depending to $model->active value.

Update DetailView:

[
    'attribute' => 'active',
    'value' => function ($model) {
        return $model->active ? "Yes" : "No";
    },
    'contentOptions' => ['style' => $model->active ? 'color:green' :'color:red']
],

Update GridView:

[
    'attribute' => 'active',
    'value' => function ($model) {
        return $model->active ? "Yes" : "No";
    },
    'contentOptions' => function ($model, $key, $index, $column) { 
        return $model->active ? ['style' => 'color:green'] : ['style' => 'color:red']; 
    }
],

Upvotes: 1

Related Questions