Answer the question
In order to leave comments, you need to log in
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]) . ' '
. Html::a('<i class="icon-remove3"></i>', ['/reviews/default/delete', 'id' => $model->review_id])
. '</div>';
},
],
]
]);
[
'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>',
],
/**
* Возвращает отформатированную дату
* @return string
*/
public static function getDateUpdate()
{
return DateFormat::load()->dateToBigString(self::date_update);
}
Undefined class constant 'date_update'
Answer the question
In order to leave comments, you need to log in
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);
}
public function getDateUpdate()
{
return DateFormat::load()->dateToBigString($this->date_update);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question