dongnan4571
2014-08-25 16:19
浏览 67

Yii迁移多个数据库 - 指定数据库?

We have a huge app built on Yii.

So far migrating was done by copy-pasting SQL dumps in phpliteadmin/phpmyadmin, but that's not very convenient.

I want to use CDbMigrations for it, with yiic migrate.

The issue is that we have two databases - a small sqlite one for config and settings, and large mysql for storing some historical records.

How do I specify what database the migration belongs to?

This is the basic migration structure, for illustration.

class do_stuff extends CDbMigration
{
    public function up()
    {
        // create table
    }

    public function down()
    {
        // drop table
    }
}

图片转代码服务由CSDN问答提供 功能建议

我们在Yii上建立了一个巨大的应用程序。

到目前为止,迁移是 通过在phpliteadmin / phpmyadmin中复制粘贴SQL转储来完成,但这不是很方便。

我想使用 CDbMigration 来实现它, yiic 迁移

问题是我们有两个数据库 - 一个用于配置和设置的小型sqlite,以及用于存储一些历史记录的大型mysql。

如何指定迁移所属的数据库?

这是基本的迁移结构,用于说明。

  class do_stuff扩展CDbMigration 
 {
公共函数up()
 {
 //创建表
} 
 
公共函数down()
 {
 // drop table 
} 
  } 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dskyx46424 2014-08-25 17:22
    已采纳

    In your config.php file, specify your database connections in the components array.

        'db1'=>array(
                    'class'=>'CDbConnection',
                    'connectionString' => 'mysql:charset=utf8mb4;host=localhost;dbname=dbname1',
                    'username' => 'root',
                    'password' => 'root',
                    'charset' => 'utf8',
                ),
        'db2'=>array(
                    'class'=>'CDbConnection',
                    'connectionString' => 'mysql:charset=utf8mb4;host=localhost;dbname=dbname2',
                    'username' => 'root',
                    'password' => 'root',
                    'charset' => 'utf8',
                ),
    

    Then in your migration, you can do the following:

    protected $dbConnection1;
    protected function getDbConnection1()
    {
        if (null !== $this->dbConnection1) {
            return $this->dbConnection1;
        }
    
        return $this->dbConnection1 = Yii::app()->getComponent('db1');
    }
    

    Repeat for your second connection.

    Then use those methods to get the CDbConnection for each database, from which you can create commands to execute.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanqujing3863 2018-10-03 14:48

    You can use CDbMigration::setDbConnection() to change the default, or override the getDbConnection() method. This is similar to another proposed solution, but it had a typo and it needed to be a public function, so I include here the fix.

    In your migration, include the following (change db1 for the defined connection name in your configuration files):

    protected $dbConnection1;
    public function getDbConnection()
    {
       if (null !== $this->dbConnection1) {
          return $this->dbConnection1;
       }
    
       return $this->dbConnection1 = Yii::app()->getComponent('db1');
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题