0

I have defined 2 databases , for example

return [
'components' => [
    'db1' => [
        'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=localhost;dbname=db1name', 
        'username' => 'db1username',
        'password' => 'db1password',
    ],
    'db2' => [
        'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=localhost;dbname=db2name', 
        'username' => 'db2username',
        'password' => 'db2password',
    ],
],
];

Now i have a table as 'users' in 'db1' and table 'countries' in 'db2'

users 
id , country_code , username , password
1  , DE           , xyz      , 12345
2  , FR           , abc      , 12345

countries
code , name
DE   , Germany
FR   , France
IN   , India

I have defined the foreign key relation between users.country_code & countries.code

ISSUE

But when i try to create the model for 'users' table using gii it gives an error , possibly because the tables relation are from 2 different databases. How to use tables from different databases in relations of a model.

Any suggestions are welcomed

7
  • I think gii uses the database connection that is stored in db. Thus it creates an error (like this). Commented Apr 5, 2016 at 9:09
  • 1
    This could help. You should overwrite getDb() in your model classes. Commented Apr 5, 2016 at 9:40
  • Hi robsch , i have already checked the links you provided but if you can let me know how to create a join statement when multiple DBs involved , for tables in a single db it is something like this - 'joinrel'=>array(self::BELONGS_TO,'User','code'), Commented Apr 5, 2016 at 10:02
  • Do you have already any code that you could provide? In SQL the tables need to be prefixed with their database names for real joins. Then it should work. Yii2 would have to realize this. I would have expected that happens if you overwrite getDb(). According to the guide I thought it would be rather simple... What have you coded so far and which error do you get? Please update your post. Commented Apr 5, 2016 at 11:41
  • When want to generate a model using Gii, there is an option which db connection to use. Default is "db" and you should write db1. Leter, when you generate your model using Gii, then you can write relation query in your User model, and that releation will reference your country model. Commented Apr 7, 2016 at 11:57

2 Answers 2

0

This works in my case to list iten on GridView::widget

-> bd_sisarc is my secound data base
-> deposito_sondagem is a table from my first data base


public static function getDb()   // on your model
{
    return Yii::$app->get('db1');
}

public static function getDb()   // on your model
{
    return Yii::$app->get('db2');
}

public function getEmpresaSondagem()  // Relation on you model
{
    return $this->hasOne(EmpresaSondagem::className(), ['idEmpSondagem' => 'entidade_deposito']);
}





public function search($params)
{


    $this->load($params);

    $sql = "SELECT deposito_sondagem.*
                  FROM
                      deposito_sondagem,
                      `bd_sisarc`.`tbempresasondagem`
                  WHERE
                      `bd_sisarc`.`tbempresasondagem`.`idEmpSondagem`=`deposito_sondagem`.`entidade_deposito`

                      and deposito_sondagem.estado=1
                      and tbempresasondagem.estado=1
                      and numero_registo LIKE '%$this->numero_registo%'
                      and nomeempsondagem LIKE '%$this->nomeEntidade%'
                      and dono_sondagem LIKE '%$this->dono_sondagem%'
                      and data_deposito LIKE '%$this->data_deposito%'";


    $query = DepositoSondagem::findBySql($sql);
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);


    if (!$this->validate()) {
        // uncomment the following line if you do not want to return any records when validation fails
        // $query->where('0=1');
        return $dataProvider;
    }


    return $dataProvider;
}
Sign up to request clarification or add additional context in comments.

Comments

-2

Try this one

SELECT `users`.* FROM `users` LEFT JOIN `db2name`.`countries` ON `users`.`country_code` = `db2name`.`countries`.`code `

1 Comment

He is asking how to create the model and you are telling him how to query the data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.