fuction()在Detailview中不起作用yii2

时间:2017-01-18 10:10:03

标签: yii2-advanced-app detailview

当我要在Detailview中显示用户详细信息时,它会抛出:

  

htmlspecialchars()期望参数1为字符串,给定对象

以下是我的Detailview代码:

view.php

        <?= DetailView::widget([
                'model' => $model,
                'attributes' => [
                    'userID',
                    'userEmail:email',
                    'userName',
                    'userMobile',
                    'userBirthDate',
                    'userGender',
                    [
                      'attribute' => 'interestName',
                      'format' => 'raw',
                      'label' => 'Interest',
                      'value' => $model->getUserinterest(),
                    ],
                      'userStatus',
                      'userType',
                ],
         ]); 

 ?>

 function getUserinterest() { 
           foreach ($model->userinterest as $userinterest) {
                $interestNames[] = $userinterest->interestName;
            }
        return implode("\n", $interestNames);
    }

2 个答案:

答案 0 :(得分:1)

由于版本2.0.11 value可以定义为闭包。将Yii版本升级到开发人员版本2.0.11+,它将起作用。

答案 1 :(得分:-2)

按照以下最终答案:

view.php

 <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            'userID',
            'userEmail:email',
            'userName',
            'userMobile',
            'userBirthDate',
            'userGender',
            [
                'attribute' => 'interestName',
                'format' => 'raw',
                'label' => 'Interest',
                'value' => $model->getviewinterest(),
            ],
           'userStatus',
           'userType',
        ],
    ]); 

 ?>

Users.php(型号)

public function getviewinterest() 
    { 
        foreach ($this->userinterest as $userinterest)
            { 
                $interestNames[] = $userinterest->interestName; 

            } 

            if(!empty($interestNames)){
                return implode("<br/>", $interestNames);
            }else{
                return "(not set)";
            }
    }