L
L
Lander2015-11-03 16:39:16
Yii
Lander, 2015-11-03 16:39:16

How to make union work in Yii2 in ActiveDataProvider?

Good afternoon. I have a situation when in GridView it is necessary to load the data from two tables. Smoking manuals for an hour gave me the opportunity to write this:

public static function getLeadQuestionProvider($pageSize = 20) 
  {
    $leadQuery = (new \yii\db\Query())
      ->select("id AS id, ".LeadQuestion::tableName().".city_id AS city_id, text AS text, link AS link, source AS source, created AS created")
      ->from(LeadQuestion::tableName())
      ->innerJoin(City::tableName(), City::tableName().'.city_id = '.LeadQuestion::tableName().'.city_id')
      ->where(['NOT IN', City::tableName().'.region_id', [3468, 4312, 3563]]);
    
    $questionQuery = (new \yii\db\Query())
      ->select("CONCAT('0') AS id, ".Questions::tableName().".city_id AS city_id, text AS text, ".Address::tableName().".phone AS link, CONCAT('LD') AS source, create_date AS created")
      ->from(Questions::tableName())
      ->innerJoin(Address::tableName(), Address::tableName().'.id = '.Questions::tableName().'.address_id')
      ->innerJoin(City::tableName(), City::tableName().'.city_id = '.Questions::tableName().'.city_id')
      ->where(['NOT IN', City::tableName().'.region_id', [3468, 4312, 3563]]);
    
    $query = (new \yii\db\Query())
      ->from(['scraper' => $leadQuery->union($questionQuery)]);
    
    return new ActiveDataProvider([
      'query' => $query,
      'pagination' => [
        'pageSize' => $pageSize
      ]
    ]);
  }

However, as I understand it, $query returns an array, and GridView is waiting for an object and the ability to add this query with its own offset, limit and orderByami. In the end it doesn't work. :( CHADNT?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2015-11-04
@webinar

Isn't it easier to make connections in models through which to pull data?
Read this: www.yiiframework.com/wiki/621/filter-sort-by-calculated - very interesting article with examples on the topic.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question