使用Doctrine从Symfony2中的外部数据库导入表,可以通过以下步骤完成:
在Symfony2项目中,首先需要安装Doctrine Migrations Bundle。可以通过以下命令安装:
composer require doctrine/doctrine-migrations-bundle
在app/config/config.yml
文件中,添加以下配置:
doctrine_migrations:
dir_name: "%kernel.root_dir%/DoctrineMigrations"
namespace: Application\Migrations
table_name: migration_versions
name: Application Migrations
在src/Application/Migrations
目录下创建一个新的目录,命名为Migrations
。
使用以下命令生成一个新的Migration文件:
php app/console doctrine:migrations:generate
在生成的Migration文件中,编辑up
和down
方法,以便在外部数据库中创建和删除表。例如:
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('CREATE TABLE my_table (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB');
}
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('DROP TABLE my_table');
}
使用以下命令执行Migration:
php app/console doctrine:migrations:migrate
这样,就可以在Symfony2项目中使用Doctrine从外部数据库导入表了。
领取专属 10元无门槛券
手把手带您无忧上云