How to select related data in Yii2?

Tell me how you can choose related, but the consolidated data was not in the extra column and rows .
for example:
model Category(id, title), Page model(id, title, category_id)
generated activequery and so the result is:
line 1 - id_1/category1/
line 2 - id_1/title1_model_page (for category 1)
line 3 - id_2/title2_model_page(for category 1)
line 4 - id_1/категория2/
line 5 - id_1/title1_model_page(for category 2)
or at least tell me how in sql it is possible to form a query that is in rows so it is necessary to collect related data.
Yes there is an option to collect all data in an array(making the loop through the list of categories and each category selecting posts), but data can be a lot and would like to use an iterator(or each batch).
And the rest of the array with the data I'm trying to push here https://github.com/codemix/yii2-excelexport to export to Excel.
July 9th 19 at 12:57
1 answer
July 9th 19 at 12:59
Solution
use UNION www.mysql.ru/docs/man/UNION.html
SELECT id, title FROM categories
UNION 
SELECT category_id as id, title FROM page

Find more questions by tags Yii