【问题标题】:Object of class Closure could not be converted to string闭包类的对象无法转换为字符串
【发布时间】:2016-11-29 07:17:54
【问题描述】:

我在 Yii 2 的 DetailView 中使用下面的代码时遇到了这个错误。

闭包类的对象无法转换为字符串

代码是:

[
    'format' => 'raw',
    'attribute' => 'title',
    'value' => function($model1, $key) {
        if ($model1->book->language == 1) {
            $m = "<p class='n'>" . $model1->book->title . "</p>";
        } else {
            $m = $model1->book->title;
        }
        return $m;
    },
    'contentOptions' => ['class' => 'text-center'],
    'headerOptions' => ['class' => 'text-center']
],

你们能帮帮我吗?

【问题讨论】:

    标签: yii2


    【解决方案1】:

    DetailView 不会像GridView 那样将闭包作为价值,只是字符串。将其更改为:

    'value' => $model1->book->language == 1 
               ? "<p class='n'>" . $model1->book->title . "</p>" 
               : $model1->book->title,
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      • 1970-01-01
      • 2022-01-05
      • 2015-07-27
      • 2012-04-29
      • 2011-04-06
      相关资源
      最近更新 更多