0
votes

I am learning Yii2, when I do listing with custom query I am getting error:

The "query" property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses.

I am getting data from query but when I put in dataprovider it is giving me error. Here is my code:

public function search($params) {
    //$query = User::find();
    $query = new \yii\db\Query;
$query = $query->select(['user.*','tbl_region.id','tbl_region.regionName']) 
      ->from('user')
      ->join('LEFT JOIN','tbl_men_reg_info','tbl_men_reg_info.userID = user.id')
          ->join('LEFT JOIN','tbl_women_info','tbl_women_info.userID = user.id')  
          ->join('LEFT JOIN','tbl_region','tbl_men_reg_info.region = tbl_region.id');
    $command = $query->createCommand();
$query = $command->queryAll();
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);
    return $dataProvider;
}

Can anyone please tell me how to do listing with custom query?

1
possible duplicate of Yii2 RESTful relational data - Armfoot
@Armfoot Yes, it's kind of similar except of data provider usage. - arogachev

1 Answers

1
votes

The error is kind of clear, you need to pass instance of yii\db\Query to data provider, so remove these lines:

$command = $query->createCommand();
$query = $command->queryAll();

and it should work.

Note that you also need to use SqlDataProvider instead of ActiveDataProvider, since models are not involved.

Also modifying variable with different type and meaning is considered bad practice:

$query = $command->queryAll();

It's not $query at this point, it's better to rename it for example as $rows or $results.