Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 DetailView: value of attribute using a function [duplicate]

Tags:

php

yii2

I get an error when I use a function to get the value of an attribute and it's working normally using Gridview. What I'm doing wrong?

<?= DetailView::widget([
    'model'      => $model,
    'attributes' => [
        [
            'label'  => 'subject_type',
            'value'  => function ($data) {
                return Lookup::item("SubjectType", $data->subject_type);
            },
            'filter' => Lookup::items('SubjectType'),
        ],
        'id',
        'subject_nature',
    ],
]) ?>
like image 551
Deena Samy Avatar asked Jan 05 '15 20:01

Deena Samy


3 Answers

I have experienced this kind of problem. The error was

PHP Warning – yii\base\ErrorException htmlspecialchars() expects parameter 1 to be string, object given

what I did was transfer the function in the model like this.

function functionName($data) {
     return Lookup::item("SubjectType", $data->subject_type);
},

and then in your view.php file..

  [
            'label'=>'subject_type',
            'value'=>$model->functionName($data),
  ],
like image 71
beginner Avatar answered Nov 15 '22 15:11

beginner


Just use call_user_func()

<?= DetailView::widget([
    'model'      => $model,
    'attributes' => [
        [
            'label'  => 'subject_type',
            'value'  => call_user_func(function ($data) {
                return Lookup::item("SubjectType", $data->subject_type);
            }, $model),
            'filter' => Lookup::items('SubjectType'),
        ],
        'id',
        'subject_nature',
    ],
]) ?>
like image 14
Alexey Berezuev Avatar answered Nov 15 '22 15:11

Alexey Berezuev


Fabrizio Caldarelli, on 05 January 2015 - 03:53 PM, said: Yes because 'value' attribute is a real value or attribute name, as doc says

So your code should be:

<?= DetailView::widget([
    'model' => $model,
    'attributes' => [
        [
            'label'  => 'subject_type',
            'value'  => Lookup::item("SubjectType", $model->subject_type),
            'filter' => Lookup::items('SubjectType'),
        ],
        'id',
        'subject_nature',
    ],
]) ?>
like image 12
Deena Samy Avatar answered Nov 15 '22 16:11

Deena Samy