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.