Yii2 Gridview : Object of class ... could not be converted to string

时间:2018-02-03 11:19:49

标签: php arrays gridview yii2 yii2-advanced-app

I tried to display data from table 'Event' to a GridView, I have tried ActiveDataProvider and ArrayDataProvider but both give me this error.

PHP Recoverable Error – yii\base\ErrorException - Object of class ... could not be converted to string

Can someone explain what should I fix from this code?

          <?= 
                $query = new Query(); 
                $dataProvider = new ArrayDataProvider([
                    'query' => $query->from('event')->all(),
                ]);

                GridView::widget([
                    'id' => 'gridEvent',
                    'dataProvider' => $dataProvider,
                    'columns' => [
                        'name',
                        'address',
                        'started_at',
                        'ended_at',
                    ],
                ]);
            ?>

1 个答案:

答案 0 :(得分:0)

您需要在顶部添加use yii\db\Query或在调用函数new \yii\db\Query()时尝试提供完整的命名空间,并且在提供->all()值时不需要query ActiveDataProvider您需要向其提供对象而不是结果集。

<?= 
    $query = new \yii\db\Query(); 

    $dataProvider = new \yii\data\ActiveDataProvider([
        'query' => $query->from('event'),
    ]);

    GridView::widget([
        'id' => 'gridEvent',
        'dataProvider' => $dataProvider,
        'columns' => [
            'name',
            'address',
            'started_at',
            'ended_at',
        ],
    ]);
?>

刚刚确认它正在我的工作,

修改

如果您仍然遇到同样的错误,请尝试运行guide-db-query-builder

提供的以下内容
$rows = (new \yii\db\Query())
->select(['id', 'email']) 
->from('user') 
->limit(10) 
->all(); 

print_r($rows);

如果它仍然说相同,则很可能您只需删除目录vendors并再次运行composer update,因为您的设置似乎有问题