Assigning function to value attribute in details v

2019-07-28 18:16发布

问题:

This question already has an answer here:

  • Changing value of an attribute in DetailView widget 2 answers

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
    ]
]); ?>