在详细视图yii2中将函数分配给值属性
Assigning function to value attribute in details view yii2
我试图在 DetailView 中将一个函数的值赋给 'value'。但是当我尝试这样做时,我得到了错误 "Object of class Closure could not be converted to string"
我也尝试通过将其分配给另一个变量来返回函数的值,但仍然出现相同的错误。
有人能帮我解决一下吗?
<?= 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',
],
]) ?>
但现在错误提示文件意外结束。
DetailView 不允许关闭该值,因为不需要它,因为 DetailView 在单个模型上运行(不会在 GridView 等许多模型上迭代)。
您的第一种方法非常糟糕,因为您试图在数组元素之间插入一些代码。第二种方法可以,但这里不允许关闭。
只需将作为 function ($model)
主体的代码(当然没有 return
部分)放在 <?= DetailView::widget(...
上方,然后使用 $employes
里面的值的变量。
[
'attribute' => 'Assign_task_to',
'format' => 'raw',
'value' => $employes,
],
您可以使用自定义函数在 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
]
]); ?>
我试图在 DetailView 中将一个函数的值赋给 'value'。但是当我尝试这样做时,我得到了错误 "Object of class Closure could not be converted to string"
我也尝试通过将其分配给另一个变量来返回函数的值,但仍然出现相同的错误。
有人能帮我解决一下吗?
<?= 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',
],
]) ?>
但现在错误提示文件意外结束。
DetailView 不允许关闭该值,因为不需要它,因为 DetailView 在单个模型上运行(不会在 GridView 等许多模型上迭代)。
您的第一种方法非常糟糕,因为您试图在数组元素之间插入一些代码。第二种方法可以,但这里不允许关闭。
只需将作为 function ($model)
主体的代码(当然没有 return
部分)放在 <?= DetailView::widget(...
上方,然后使用 $employes
里面的值的变量。
[
'attribute' => 'Assign_task_to',
'format' => 'raw',
'value' => $employes,
],
您可以使用自定义函数在 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
]
]); ?>