title: Yii2 - Union query with Pagination into ActiveRecord
body: <p>I have a case where I need to have a <strong>union query</strong>, and at the same time have the end result as <strong>ActiveRecord</strong>. To illustrate, I have two tables <strong>Products</strong> and <strong>Categories</strong>. Each has a model that extends from ActiveRecord. I'm trying to have a query as follows</p>
  
  <pre><code>$deletedProducts = Products::find()-&gt;select([$productsTable . '.id', $productsTable . '.selling_price'])
                  -&gt;where(['client' =&gt; $clientId])
                  -&gt;andWhere(['&lt;&gt;', 'hidden', 0]);
  </code></pre>
  
  <p>and </p>
  
  <pre><code>$deletedCats = Category::find()-&gt;select([$catTable . '.id', $catTable . '.selling_price'])
                  -&gt;where(['client' =&gt; $clientId])
                  -&gt;andWhere(['&lt;&gt;', 'hidden', 0]);
  </code></pre>
  
  <p>and </p>
  
  <pre><code>$archivedItemsQuery = $deletedProducts-&gt;union($deletedCats);
  </code></pre>
  
  <p>I then return the results through <strong>ActiveDataProvider</strong></p>
  
  <pre><code>$dataProvider = new ActiveDataProvider(
              [
                  'query' =&gt; $archivedItemsQuery,
          ]
      ]);
  return $dataProvider-&gt;getModels();
  </code></pre>
  
  <p>The problem is that when I try to send pagination data in the request, or set it through the ActiveDataProvider, no pagination happens. I can change this query of course to </p>
  
  <pre><code>$unionQuery = (new \yii\db\Query())
                  -&gt;from(['counter_items' =&gt; $archivedItemsQuery]);
  </code></pre>
  
  <p>This results in correct pagination, but I dont get the result as <strong>ActiveRecords</strong>. </p>
  
  <p>Any ideas on how to create a union query with <strong>1. Pagination working and 2. The result is ActiveRecord.</strong></p>