Yii2 ActiveDataProvider通过ActiveQueryInterface查询(查询关联) - php

ActiveDataProvider实现基于yii \ db \ Query和yii \ db \ ActiveQuery的数据提供程序。

但是,参数$ query仅实现QueryInterface,而不实现ActiveQueryInterface,这对ActiveDataProvider来说很奇怪!

provider = new ActiveDataProvider([
    'query' => MainModel::find()
              ->with("relatedModel")
              ->where(["relatedModel.something"=>$value]),
]);

参考方案

最近4个小时,我一直在同一个问题上挣扎。解决方案是使用joinWith()代替with():

$provider = new ActiveDataProvider([
    'query' => MainModel::find()
              ->joinWith("relatedModel") //here change the with() to joinWith()
              ->where(["relatedModel.something"=>$value]),
]);

45码