目前的項目結構是這樣的(參照代碼庫):php
其中,db/migrations
文件夾是遷移類文件夾,config/db.php
是咱們項目原有的db配置,migrations.php
和migrations-db.php
是遷移組件須要的配置文件。html
如今先在根目錄新建文件:migrate,沒有後綴名,而且添加可執行權限。mysql
而且參照組件原有的命令腳本vendor/doctrine/migrations/doctrine-migrations.php
,首先獲取項目原有的數據庫配置信息,替換掉migrations-db.php數據庫配置文件:git
$db_config = include 'config/db.php'; $db_params = [ 'driver' => 'pdo_mysql', 'host' => $db_config['host'], 'port' => $db_config['port'], 'dbname' => $db_config['dbname'], 'user' => $db_config['user'], 'password' => $db_config['password'], ]; try { $connection = DriverManager::getConnection($db_params); } catch (DBALException $e) { echo $e->getMessage() . PHP_EOL; exit; }
而後配置組件,替換掉migrations.php配置文件:github
$configuration = new Configuration($connection); $configuration->setName('Doctrine Migrations'); $configuration->setMigrationsNamespace('db\migrations'); $configuration->setMigrationsTableName('migration_versions'); $configuration->setMigrationsDirectory('db/migrations');
最後是完整的命令腳本代碼:sql
#!/usr/bin/env php <?php require_once 'vendor/autoload.php'; use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Migrations\Configuration\Configuration; use Doctrine\DBAL\Migrations\Tools\Console\ConsoleRunner; use Doctrine\DBAL\Migrations\Tools\Console\Helper\ConfigurationHelper; use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper; use Symfony\Component\Console\Helper\HelperSet; use Symfony\Component\Console\Helper\QuestionHelper; // 讀取數據庫配置信息 $db_config = include 'config/db.php'; $db_params = [ 'driver' => 'pdo_mysql', 'host' => $db_config['host'], 'port' => $db_config['port'], 'dbname' => $db_config['dbname'], 'user' => $db_config['user'], 'password' => $db_config['password'], ]; try { $connection = DriverManager::getConnection($db_params); } catch (DBALException $e) { echo $e->getMessage() . PHP_EOL; exit; } // 遷移組件配置 $configuration = new Configuration($connection); $configuration->setName('Doctrine Migrations'); $configuration->setMigrationsNamespace('db\migrations'); $configuration->setMigrationsTableName('migration_versions'); $configuration->setMigrationsDirectory('db/migrations'); // 建立命令腳本 $helper_set = new HelperSet([ 'question' => new QuestionHelper(), 'db' => new ConnectionHelper($connection), new ConfigurationHelper($connection, $configuration), ]); $cli = ConsoleRunner::createApplication($helper_set); try { $cli->run(); } catch (Exception $e) { echo $e->getMessage() . PHP_EOL; }
如今執行遷移相關命令時,用./migrate
替換以前的vendor/bin/doctrine-migrations
部分便可。數據庫
同時migrations.php
和migrations-db.php
這兩個配置文件也能夠刪除了。segmentfault
若是你使用PhpStorm,命令行還能夠集成到開發工具中去,會有自動提示,大大提升工做效率:app
PhpStorm
-> Preferences
-> Command Line Tool Support
-> 添加PhpStorm
-> Tools
-> Run Command
,彈出命令窗口,輸入m即會出現命令提示。到此,數據遷移組件就已經很靈活的集成到咱們本身的項目中了,並且你還能夠根據本身的項目靈活修改自定義命令腳本文件。ide
可是若是使用久了會發現如今的數據遷移會有兩個問題:
不支持tinyint類型,在相關issues中,官方開發人員有回覆:
"Tiny integer" is not supported by many database vendors. DBAL's type system is an abstraction layer for SQL types including type conversion from PHP to database value and back. I am assuming that your issue refers to MySQL, where we do not have a distinct native SQL type to use for BooleanType mapping. Therefore MySQL's TINYINT is used for that purpose as is fits best from all available native types and in DBAL we do not have an abstraction for tiny integers as such (as mentioned above).What you can do is implement your own tiny integer custom type and tell DBAL to use column comments (for distinction from BooleanType in MySQL). That should work. See the documentation.
To tell DBAL to use column comments for the custom type, simply override the requiresSQLCommentHint() method to return true.
因此只能添加自定義類型。
那麼,下一章咱們就來完成這兩件事:添加tinyint的自定義類型,解決enum報錯的問題。
在 個人代碼庫能夠查看這篇文章的詳細代碼,歡迎star。