YII2在2个数据库的表之间的模型中创建关系

YII2在2个数据库的表之间的模型中创建关系,yii2,relationship,yii2-advanced-app,multiple-databases,yii2-model,Yii2,Relationship,Yii2 Advanced App,Multiple Databases,Yii2 Model,例如,我定义了两个数据库 return [ 'components' => [ 'db1' => [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=db1name', 'username' => 'db1username', 'password' => 'db1password', ],

例如,我定义了两个数据库

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',
    ],
],
];
现在,我在“db1”中有一个表作为“users”,在“db2”中有一个表作为“countries”

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

countries
code , name
DE   , Germany
FR   , France
IN   , India
我已经定义了users.country\u code和countries.code之间的外键关系

问题

但是当我尝试使用gii为“users”表创建模型时,它给出了一个错误,可能是因为表关系来自两个不同的数据库。 如何在模型的关系中使用来自不同数据库的表

欢迎提出任何建议

试试这个

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

在我的例子中,这可以在GridView::widget上列出iten

-> 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;
}

我认为gii使用存储在
db
中的数据库连接。因此,它会产生一个错误(如)。我可以帮忙。您应该在模型类中覆盖。嗨,robsch,我已经检查了您提供的链接,但是如果您能让我知道如何在涉及多个db时创建join语句,对于单个db中的表,它是这样的-'joinrel'=>array(self::belishing_to,'User','code'),您是否已经有了可以提供的代码?在SQL中,表需要以其数据库名称作为前缀,以实现真正的连接。那么它应该会起作用。Yii2必须意识到这一点。如果覆盖getDb(),我会预料到会发生这种情况。根据我的想法,这会很简单。。。到目前为止,您编写了哪些代码,出现了哪些错误?请更新您的帖子。当您想使用Gii生成模型时,有一个选项可以使用db连接。默认值为“db”,应该写入db1。Leter,当您使用Gii生成模型时,您可以在您的用户模型中编写关系查询,该关联将引用您的国家模型。他询问如何创建模型,您告诉他如何查询数据。