Assigning function to value attribute in details view yii2 [duplicate]

六眼飞鱼酱① 提交于 2019-12-11 06:49:44

问题


I am trying to assign a value of a function to 'value' in DetailView. But when I try that I get the error "Object of class Closure could not be converted to string"

I tried returning the value of the function by assigning it to another variable too but still the same error.

Can someone help me figure it out please?

 <?= DetailView::widget([
    'model' => $model,
    'attributes' => [
        'Task_ID',
        'Task_Title',
        'Description',
        'projects.project_name', //display project name instead of project id
       // 'Assign_task_to',
        //'tasksemp.Employee_ID',
        [
            'attribute' => 'Assign_task_to',
            'format' => 'raw',

            $data = function ($model) {
                $assignEmpModel = Tasksemp::find()->where(['Task_ID' => $model->Task_ID])->all(); //get all the rows with teh same task_id
                $employes = ''; //empty string
                foreach ( $assignEmpModel as $employee ) {
                    $emp = Employee::find()->where(['Employee_ID'=>$employee->Employee_ID])->one(); //get all the employee_id 
                    $name = $emp['employee_name']; //get the employee_name from the employees table
                    $employes .= ' '.$name.', '; //concatenate the names.
                }
            //  return implode(', ',$employes);
            //return $employes;
            },
            'value' => $data,
        ],
        'start_date',
        'due_date',
        'priotiy_level',
        'upload_documents',
    ],
]) ?>

I have tried assigning the function to a variable $data but doesn't work.

I have tried assigning the function directly to the value as well but same error.

[
            'attribute' => 'Assign_task_to',
            'format' => 'raw',
            'value' => function ($model) {   //<----- ERROR HERE!
                $assignEmpModel = Tasksemp::find()->where(['Task_ID' => $model->Task_ID])->all(); //get all the rows with teh same task_id
                $employes = ''; //empty string
                foreach ( $assignEmpModel as $employee ) {
                    $emp = Employee::find()->where(['Employee_ID'=>$employee->Employee_ID])->one(); //get all the employee_id 
                    $name = $emp['employee_name']; //get the employee_name from the employees table
                    $employes .= ' '.$name.', '; //concatenate the names.

                }
                return $employes;
            },
      ],

Tried Bizley's solution like this:

<?php $assignEmpModel = Tasksemp::find()->where(['Task_ID' => $model->Task_ID])->all(); //get all the rows with teh same task_id
                $employes = ''; //empty string
                foreach ( $assignEmpModel as $employee ) {
                    $emp = Employee::find()->where(['Employee_ID'=>$employee->Employee_ID])->one(); //get all the employee_id 
                    $name = $emp['employee_name']; //get the employee_name from the employees table
                    $employes .= ' '.$name.', '; //concatenate the names.
?>

<?=  DetailView::widget([
    'model' => $model,
    'attributes' => [
        'Task_ID',
        'Task_Title',
        'Description',
        'projects.project_name', //display project name instead of project id
       // 'Assign_task_to',
        //'tasksemp.Employee_ID',
        [
            'attribute' => 'Assign_task_to',
            'format' => 'raw',
            'value' => $employes,
        ],
        'start_date',
        'due_date',
        'priotiy_level',
        'upload_documents',
    ],
]) ?>

But now the error says unexpected end of file.


回答1:


DetailView doesn't allow closure for the value because there is no need for it since DetailView operates on single model (doesn't iterate on many models like GridView).

Your first approach is totally bad because you try to push some code between the array elements. Second approach is ok but no closure allowed here.

Simply just take the code that is the body of your function ($model) (of course without the return part) and put it above the <?= DetailView::widget(... and then use the $employes variable for the value inside.

[
    'attribute' => 'Assign_task_to',
    'format' => 'raw',
    'value' => $employes,
],



回答2:


You can use custom functions to get a value in DetailView.

Use call_user_func() to achieve this:

<?= \yii\widgets\DetailView::widget([
    'model' => $yourModel,
    'attributes' => [
        [
            'label' => 'Your label',
            'value' => call_user_func(function ($model) {
                // do what you like
                // return your value;
            }, $yourModel),
        ],
        // more attributes
    ]
]); ?>


来源:https://stackoverflow.com/questions/39266846/assigning-function-to-value-attribute-in-details-view-yii2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!