Yii2 迁移灵活的数据库引擎
Yii2 migration flexible Database Engine
我在 PHP 中有一个框架 Yii2
的应用程序。我希望此应用程序成为 运行 某些数据库引擎,例如 Postgresql、MySql 或 SQL 服务器。问题是如何迁移它。例如,您知道 Postgresql 和 SQL 服务器使用模式作为数据库的子集,而不是 MySQL.
我正在使用 Postgresql 开发我的应用程序,其中包含一些模式,例如 history
、main
和 log
。现在我想用 MySQL 尝试 运行 我的应用程序。我必须怎么做才能实现这一目标?
POSTGRESQL
my_database -> database name
main -> schema
tables
history -> schema
tables
我希望我的迁移可以运行宁任何你使用的数据库引擎(postgresql、mssql 或mysql),没有控制器/模型/等
您可以扩展 yii\db\Migration
并根据您在每个方法中使用的数据库系统类型添加条件。
例如,对于应用于所有方法的默认值,我扩展了 yii\db\Migration
并覆盖了 init()
public function init()
{
parent::init();
switch ($this->db->driverName) {
case 'mysql':
$this->tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
$this->dbType = 'mysql';
break;
case 'pgsql':
// specifics for pgsql
default:
throw new \RuntimeException('Your database is not supported!');
}
}
那么你所有的迁移都应该扩展这个。
据此,通过相同的迁移,您可以针对特定的数据库系统进行特定的操作。
我在 PHP 中有一个框架 Yii2
的应用程序。我希望此应用程序成为 运行 某些数据库引擎,例如 Postgresql、MySql 或 SQL 服务器。问题是如何迁移它。例如,您知道 Postgresql 和 SQL 服务器使用模式作为数据库的子集,而不是 MySQL.
我正在使用 Postgresql 开发我的应用程序,其中包含一些模式,例如 history
、main
和 log
。现在我想用 MySQL 尝试 运行 我的应用程序。我必须怎么做才能实现这一目标?
POSTGRESQL
my_database -> database name
main -> schema
tables
history -> schema
tables
我希望我的迁移可以运行宁任何你使用的数据库引擎(postgresql、mssql 或mysql),没有控制器/模型/等
您可以扩展 yii\db\Migration
并根据您在每个方法中使用的数据库系统类型添加条件。
例如,对于应用于所有方法的默认值,我扩展了 yii\db\Migration
并覆盖了 init()
public function init()
{
parent::init();
switch ($this->db->driverName) {
case 'mysql':
$this->tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
$this->dbType = 'mysql';
break;
case 'pgsql':
// specifics for pgsql
default:
throw new \RuntimeException('Your database is not supported!');
}
}
那么你所有的迁移都应该扩展这个。
据此,通过相同的迁移,您可以针对特定的数据库系统进行特定的操作。