通过ActiveQueryInterface查询Yii2 ActiveDataProvider(查询关系)

ActiveDataProvider implements a data provider based on yii\db\Query and yii\db\ActiveQuery.

However the parameter $query implements only the QueryInterface and not the ActiveQueryInterface which is kind of strange for ActiveDataProvider!.. Is there any way to do queries with relations like this?

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

Since I can't comment, I'll add an answer...

Did you test this code? This should work. Look at the source of ActiveDataProvider at this line and this line. They chek if $this->query is an instance of ActiveQueryInterface and do then some sort of things. Since ActiveQueryInterface extends Queryinterface your example should work. If not, what kind of error do you get?

I think if you don't use an ActiveQuery, you will not be able to sort easily when I look into the setSort method...

I struggled with a same problem for the last 4hours. The solution is to use joinWith() instead of with():

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