Answer the question
In order to leave comments, you need to log in
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
]
]);
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question