在详细信息视图yii2中将功能分配给值属性 [英] Assigning function to value attribute in details view yii2
问题描述
我正在尝试在DetailView中将函数的值分配给值。但是,当我尝试得到错误无法将类Closure的对象转换为字符串时
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.
有人可以帮我解决吗?
<?= 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,但不起作用。
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;
},
],
尝试过Bizley的解决方案,如下所示:
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.
推荐答案
DetailView不允许闭包该值,因为由于DetailView在单个模型上运行(在诸如GridView之类的许多模型上不会迭代),因此不需要此值。
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.
只需使用代码即可,该代码就是您的函数($ model)的主体
(当然不包含 return
部分),并将其置于<?= DetailView :: widget(...
,然后将 $ employes
变量用作内部值。
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,
],
这篇关于在详细信息视图yii2中将功能分配给值属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!