M
M
Maila2017-07-04 18:56:13
Yii
Maila, 2017-07-04 18:56:13

Yii2 advanced model search - how to fix error?

The following error appears in the admin panel admin.site.com/blog/index : f4cc0037621d460cb4553d17eace0ef1.jpg Code Blog.php
(D:\sites\site\yii2\common\models)

<?php

namespace common\models;

use Yii;
use common\models\Blog;

/**
 * This is the model class for table "blog".
 *
 * @property integer $id
 * @property string $title
 * @property string $text
 * @property string $url
 * @property integer $status_id
 * @property integer $sort
 */
class Blog extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'blog';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title', 'url'], 'required'],
            [['text'], 'string'],
            [['status_id', 'sort'], 'integer'],
            [['sort'], 'integer','max'=>99, 'min'=>1],
            [['title', 'url'], 'string', 'max' => 150],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Заголовок',
            'text' => 'Текст',
            'url' => 'ЧПУ',
            'status_id' => 'Статус',
            'sort' => 'Сортировка',
        ];
    }

    public static function getStatusList() {
        return ['off','on'];
}
    public function getStatusName(){
       $list = self::getStatusList();
       return $list($this->status_id);
    }
}
index.php file (D:\sites\site\yii2\backend\views\blog)
<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
/* @var $this yii\web\View */
/* @var $searchModel common\models\BlogSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Blogs';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="blog-index">

     
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <p>
        <?= Html::a('Create Blog', ['create'], ['class' => 'btn btn-success']) ?>
    </p>
<?php Pjax::begin(); ?>  


 <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            'id',
            'title',
            'url:url',
/* ['attribute'=>'status_id','filter'=>['off','on'],'value'=>function ($model){
     $status = 'off';
    if($model->status_id ==1) {
        $status = 'on';
         }
    return $status; 
 }], */

['attribute'=>'status_id','filter'=>\common\models\Blog::getStatusList(),'value'=>function ($model){
     return $model->statusName;
 }],
     'sort',
            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>

<?php Pjax::end(); ?></div>

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
S
Snewer, 2017-07-04
@Maila

return $list($this->status_id);toreturn $list[$this->status_id];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question