areff areff - 1 month ago 27
PHP Question

Call to a member function union() on a non-object in yii2

I want to create pagination using union of two tables, but get the following error:


Call to a member function union() on a non-object


$query1=Mobile::find()->select('video')->all();
$query2=Tablet::find()->select('video')->all();
$count=Mobile::find()->count();
$query = (new Query)
->select('*')
->from([
$query1->union($query2),
])
->limit(3);


How can i union two tables in this framework?

Answer Source

Try This :

$query1=Mobile::find()->select('video');
$query2=Tablet::find()->select('video');

$unionQuery = $query1->union($query2)->all()->limit(3);