我有一个带连接的mysql查询,它工作正常。我想将结果传递给Yii2 ActiveDataProvider。 我的Controller代码看起来像
`
public function actionIndex()
{
$theme = Theme::find();
$query = "SELECT t.*,COUNT(d.id) AS total_downloads FROM `themes` AS T
LEFT JOIN downloads AS D
ON D.theme_id = T.id GROUP
by t.id ORDER BY total_downloads DESC LIMIT 6";
$connection=Yii::$app->db;
$trends = $connection->createCommand($query);
$model = $trends->queryAll();
$object = (object) $model;
$ActiveDataProvider = new ActiveDataProvider(['query' => $object]);
return $this->render('trendings',[
'ActiveDataProvider'=>$ActiveDataProvider,
]);
}`
并查看文件代码`
ListView::widget([
'dataProvider' => $ActiveDataProvider,
'options' => [
'tag' => 'div',
'class' => 'list-wrapper',
'id' => 'list-wrapper',
],
'pager' => [
'firstPageLabel' => 'first',
'lastPageLabel' => 'last',
'prevPageLabel' => '<span class="glyphicon glyphicon-chevron-left"></span>',
'nextPageLabel' => '<span class="glyphicon glyphicon-chevron-right"></span>',
'maxButtonCount' => 3,
],
// 'layout' => "{pager}\n{items}\n{summary}",
'summary' => false,
'itemView' => '_list',
]);
?>`
由于我已经通过了QueryAll,因此ActiveDataProvider没有调用并传递错误&#34;&#34;查询&#34; property必须是实现QueryInterface的类的实例,例如yii \ db \ Query或其子类&#34;
答案 0 :(得分:0)
你没有通过queryAll()
,你传递了一个函数queryAll()
的结果,它是一个对象数组。并且不使用它:
$theme = Theme::find();
改变这个:
$query = "SELECT t.*,COUNT(d.id) AS total_downloads FROM `themes` AS T
LEFT JOIN downloads AS D
ON D.theme_id = T.id GROUP
by t.id ORDER BY total_downloads DESC LIMIT 6";
为:
$theme->select = 'YOUR SELECT HERE';
不要使用queryAll()
,只需将$theme
对象传递给query
param做ActiveDataProvider
。
删除它:
$connection=Yii::$app->db;
$trends = $connection->createCommand($query);
$model = $trends->queryAll();
$object = (object) $model;