【问题标题】:Assigning function to value attribute in details view yii2 [duplicate]在详细信息视图yii2中为值属性分配函数[重复]
【发布时间】:2017-01-09 01:28:42
【问题描述】:

我正在尝试将函数的值分配给 DetailView 中的“值”。但是当我尝试时,我得到错误“类闭包的对象无法转换为字符串”

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

有人可以帮我弄清楚吗?

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

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

【问题讨论】:

  • 你不应该把这样的逻辑(使SQL查询填充$employees变量)放在视图中,这是模型或单独组件的工作。 View 只负责显示和格式化。这违反了 MVC 原则。

标签: php yii2 yii2-advanced-app detailview


【解决方案1】:

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

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

只需将作为function ($model) 主体的代码(当然没有return 部分)放在&lt;?= DetailView::widget(... 上方,然后使用$employes 变量作为内部值。

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

【讨论】:

  • 我试过了,但又出现了另一个错误。文件意外结束。
  • 我告诉过你只使用函数体,而不是整个函数。
  • 我也试过了。但文件仍然意外结束..
  • 在上面的问题中粘贴你的代码。
  • foreach 循环未关闭。
【解决方案2】:

您可以使用自定义函数来获取 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
    ]
]); ?>

【讨论】:

    猜你喜欢
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    相关资源
    最近更新 更多