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',
],
]);
?>
答案 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
,因为您的设置似乎有问题