Symfony2 Doctrine Migration - 迁移文件中的内容

时间:2014-07-29 15:29:34

标签: symfony doctrine migration entity

我的symfony项目中有3个实体管理器,我想将新表添加到特定的实体管理器。 我知道我可以通过传递--em =" EntityManager1"从命令行执行此操作。使用命令:

php app/console doctrine:migration:migrate --em="EntityManager1"

但我想在代码本身中这样做。

示例:

public function up(Schema $schema)
{
    $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql");�
    $this->addSql("CREATE TABLE `myTable` (
             `id` int(11) NOT NULL,
             `name` varchar(45) DEFAULT NULL,
             `email` varchar(45) DEFAULT NULL,
              PRIMARY KEY (`id`)
              ) ENGINE=InnoDB DEFAULT CHARSET=latin1�");
}

public function down(Schema $schema)
{
    $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql"); 
    $this->addSql("DROP TABLE myTable");�
}

先谢谢

1 个答案:

答案 0 :(得分:6)

根据https://github.com/doctrine/DoctrineMigrationsBundle/issues/38

您必须实施Symfony\Component\DependencyInjection\ContainerAwareInterface

use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class Version20140717170412 extends AbstractMigration implements ContainerAwareInterface
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * {@inheritdoc}
     */
    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }

然后

/**
 * {@inheritdoc}
 */
public function up(Schema $schema)
{

    $myEmConnection = $this->container->get('doctrine')
        ->getManager('my_em')->getConnection();

    $this->skipIf(
        $this->connection !== $myEmConnection,
        'Require my_em manager'
    );