在详细信息视图yii2中为value属性赋值

时间:2016-09-01 08:49:00

标签: php yii2 yii2-advanced-app detailview

我正在尝试将函数的值赋给' value'在DetailView中。但是,当我尝试我得到错误"类Closure的对象无法转换为字符串"

我尝试通过将函数的值分配给另一个变量来返回函数的值,但仍然是相同的错误。

有人可以帮我解决一下吗?

 <?= 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',
    ],
]) ?>

我尝试将函数分配给变量$ data但不起作用。

我已经尝试将函数直接分配给值但是同样的错误。

[
            '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;
            },
      ],

尝试过Bizley这样的解决方案:

<?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',
    ],
]) ?>

但现在错误表示意外的文件结束。

2 个答案:

答案 0 :(得分:2)

DetailView不允许关闭值,因为不需要它,因为DetailView在单个模型上运行(不在像GridView这样的许多模型上迭代)。

你的第一种方法非常糟糕,因为你试图在数组元素之间推送一些代码。第二种方法是可以的,但这里不允许关闭。

只需获取function ($model)正文的代码(当然没有return部分)并将其放在<?= DetailView::widget(...上方,然后使用$employes内部值的变量。

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

答案 1 :(得分:0)

您可以使用自定义函数在DetailView中获取值。

使用call_user_func()实现此目的:

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