I need to insert such a Sql request into ActiveDataProvider , tell me how?

 SELECT * FROM ( SELECT id, username, text, sender_id FROM pm WHERE user_id =2 ORDER BY created_at DESC ) AS tmp_table GROUP BY sender_id 

Got to this

 $subQuery = (new \yii\db\Query())->select('id, username, text, sender_id')->from('pm')->where('user_id =2 ORDER BY created_at DESC'); $query = self::find(); $query->from(['AS tmp_table GROUP BY sender_id' => $subQuery]); $query->with(['profile']); 

But this code generates such a request.

 SELECT * FROM ( SELECT `id` ,`username` ,`text` ,`sender_id` FROM `pm` WHERE user_id =2 ORDER BY created_at DESC ) `AS tmp_table GROUP BY sender_id` LIMIT 10 

It does not work due to the fact that it puts the apotof in front of AS AS tmp_table GROUP .


here is all the code, there's a mistake somewhere, maybe someone will see

 $subQuery = (new \yii\db\Query()) ->from('pm') ->where('user_id = :user_id AND sender_id != :user_id AND status != :status ORDER BY created_at DESC', [ 'user_id' => Yii::$app->user->id, 'status' => Pm::STATUS_DELETE]); $query = self::find()->select('id, text, created_at, username, sender_id'); $query->from(['AS `tmp_table` GROUP BY `sender_id` ORDER BY `created_at` DESC' => $subQuery]); $query->with(['profile']); $dataProvider = new ActiveDataProvider([ 'query' => $query, 'pagination' => [ 'pageSize' => 10, ], ]); $this->load($params); // grid filtering conditions $query->andFilterWhere([ 'FROM_UNIXTIME(created_at, "%d.%m.%Y")' => $this->created_at, ]); // $query->andFilterWhere(['like', 'text', $this->text]) // ->andFilterWhere(['like', 'username', $this->username]); return $dataProvider; 
  • Try this: $subQuery = (new Query())->select('SELECT id, username, text, sender_id')->from('pm')->where('user_id =2')->orderBy('created_at DESC'); and the main query $query = (new Query())->from(['tmp_table' => $subQuery])->groupBy('sender_id'); ... here is a similar example shown yiiframework.com/doc-2.0/guide-db-query-builder.html github.com/yiisoft/yii2/blob/master/docs/guide/… - Alexey Shimansky
  • Try tmp_table apostrophes. When I needed to connect the table with myself, I was perverted like this: ->innerJoin('(``table`` AS ``alias``)', ..., ...) (Replace the double apostrophe with a single) - LANSELOT
  • @LANSELOT why do you need to be perverted, if the alias is done like this 'table' => 'alias' .... github.com/yiisoft/yii2/blob/master/docs/guide/… - Alexey Shimansky
  • @ Alexey Shimansky Please post your comment as a response. - Nicolas Chabanovsky
  • LANSELOT Respect to you and uvazhuha! apotrof helped !!! - Kostya

0