I have 2 models on my search function joined by a field an I want to create an ActiveDataProvider
for my GridView
. This is what I have so far:
My yii db config:
db => users logs data;
db1 => system data;
Model1
and Model2
are in db1
public function search($params)
{
// (...)
$query1 = Model1::find();
$query2 = Model2::find();
$query1->select(
new Expression ("'Data model1: ' Model"),
'model1.id',
'model1.columns',
)->jointWith(['mODEL2'])
->andWhere(new Expression ('model1.id || model1.columns != model2.id || model2.columns'));
$query2->select(
new Expression ("'Data model2: ' Model"),
'model2.id',
'model2.columns',
)->jointWith(['mODEL1'])
->andWhere(new Expression ('model1.id || model1.columns != model2.id || model2.columns'));
$unionQuery = (new Query())->from(['dummy_name'=>$query1->union($query2)]);
$dataProvider = new ActiveDataProvider([
'query'=>$unionQuery,
]);
// (...)
return $dataProvider;
}
And the server database exception:
the table or view not exist
In my models (1 and 2) I do specific that I use the db1
connection, but how can I do that in the $unionQuery
without use $unionQuery->all(Yii::$app->db1)
?
That do not work.
In your case query is executed at ActiveDataProvider level so you should set ActiveDataProvider::$db property with correct db connection.
awesome thanks a lot this works
$dataProvider = new ActiveDataProvider([
'query'=>$unionQuery,
'db'=>Yii::$app->db1,
]);