ActiveDataProvider:"query" 属性必须是实现 QueryInterface 的类的实例

reg*_*tcn 2 yii yii-extensions yii2

我正在使用一个自定义的函数,它封装了 findBySql() 来返回行。但是在标题中显示了错误。但是如果我测试使用封装 find() 的自定义函数,它起作用了,为什么?

这是我的行动:

public function actionList()
{
     $model = new Loan();
     $dataProvider = new ActiveDataProvider(
     [
        'query' => $model->findValid($_GET['type']),//error comes here
        'pagination' => [
                        'pagesize' => '2',
         ],
     ]);

      return $this->renderPartial('list', ['model' => $model, 'dataProvider' => $dataProvider]);
}
Run Code Online (Sandbox Code Playgroud)

这是对象贷款的 findxx 函数:

 public function findValid($type=null)
{
    if($type==null){
            return static::findBySql("select * from loan where wmstat&1=1 and (wmstat>>2)&1=0")->all();
    }else{
            return static::findBySql("select * from loan where wmstat&1=1 and (wmstat>>2)&1=0 and origin="."'".$type."'")->all();
    }
}
Run Code Online (Sandbox Code Playgroud)

此外,我可以使用 find() 和 where() 更改位操作并达到相同的效果吗?

aro*_*hev 5

错误很明显,query需要有效的查询实例,而您传递的是查询结果而不是查询本身。

删除方法中的->all()调用,findValid()它应该可以工作。

PS我强烈建议重构您的代码以更好地条件。

官方文档: