N
N
nepster-web2014-05-07 20:42:54
Yii
nepster-web, 2014-05-07 20:42:54

Yii2, How to access a model property by calling a getter?

I have a feedback table that is generated using gridview:

echo GridView::widget([
    'id' => 'pages-grid',
    'dataProvider' => $dataProvider,
    'filterModel'  => $searchModel,
    
    //'options'      => ['class' => ''],
    'tableOptions' => ['class' => 'table table-bordered'],
    //'rowOptions'   => ['class' => ''],
    
    'layout' => '<div class="GridViewSummary">{summary}</div><div class="panel panel-default"><div class="table-responsive">{items}</div><div class="table-footer">{pager}</div></div>',
    
    'columns' => [
        [
            'attribute' => 'user',
            'format' => 'html',
            'label'  => Yii::t('articles.main', 'Автор'),
            'contentOptions' => ['class'=> 'tdAuthor'],
            'value' => function ($model) {
                return UserColumn::widget([
                    'userId' => $model->user_id
                ]);
            }
        ],
        [
            'attribute' => 'text',
            'contentOptions' => ['class'=> 'tdText'],
            'format' => 'html',
            'value' => function ($model) {
                return '<div class="text-left">' . $model->text .  '</div>';
            }
        ],
        
        [
            'attribute' => 'date_create',
            'format' => 'html',
            'contentOptions' => ['class'=> 'tdDate'],
            'value' => function ($model) {
                return '<div class="text-center">' . $model->dateCreate . '</div>';
            },
            'filter' => '<div class="text-center">' . Yii::t('app','Нет фильтра') . '</div>',
        ],
        
        [
            'attribute' => 'date_update',
            'format' => 'html',
            'contentOptions' => ['class'=> 'tdDate'],
            'value' => function ($model) {
                return '<div class="text-center">' . $model->dateUpdate . '</div>';
            },
            'filter' => '<div class="text-center">' . Yii::t('app','Нет фильтра') . '</div>',
        ],
        
        [
            'format' => 'html',
            'contentOptions' => ['class'=> 'tdControl'],
            'label'  => Yii::t('app', 'Управление'),
            'value' => function ($model) {           
                return '<div class="text-center">' 
                            . Html::a('<i class="icon-wrench"></i>', ['/reviews/default/update', 'id' => $model->review_id])  . ' &nbsp; '
                            . Html::a('<i class="icon-remove3"></i>', ['/reviews/default/delete', 'id' => $model->review_id]) 
                       . '</div>';
            },
        ],
    ]
]);

There are two fields, date added and date edited. Previously I did this:
[
            'attribute' => 'date_update',
            'format' => 'html',
            'contentOptions' => ['class'=> 'tdDate'],
            'value' => function ($model) {
                return '<div class="text-center">' . DateFormat::load()->dateToBigString($this->date_update) . '</div>';
            },
            'filter' => '<div class="text-center">' . Yii::t('app','Нет фильтра') . '</div>',
        ],

This method works fine, but I tried to put this case in a getter.
In the main model I did the following:
/**
   * Возвращает отформатированную дату
     * @return string
   */
  public static function getDateUpdate()
  {
    return DateFormat::load()->dateToBigString(self::date_update);
  }

And actually, they answered me:
Undefined class constant 'date_update'
To be honest, I did not understand why this happens, and is it possible to make a getter that would return a formatted date?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cage, 2014-05-07
@nepster-web

You first decide what you want to receive as a result of calling the static (by the way, why static?) GetDateUpdate method?
options:
1. Some date from a constant defined inside the model class, as it is now (only the constant is not defined).
2. Some date from the static property of the model class, then the code will look like this:

public static function getDateUpdate()
{
  return DateFormat::load()->dateToBigString(self::$date_update);
}

3. All the same, the value of the attribute of a specific model object, then no statics and the getter will look like this:
public function getDateUpdate()
{
    return DateFormat::load()->dateToBigString($this->date_update);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question