Php 使原则迁移符合ContainerWareInterface

Php 使原则迁移符合ContainerWareInterface,php,symfony,doctrine,database-migration,Php,Symfony,Doctrine,Database Migration,我有一个原则迁移,看起来像这样: <?php namespace Application\Migrations; use Doctrine\ORM\Mapping\PushNotification; use Doctrine\DBAL\Migrations\AbstractMigration; use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Doctrine\DBAL\Schema\Schem

我有一个原则迁移,看起来像这样:

<?php

namespace Application\Migrations;

use Doctrine\ORM\Mapping\PushNotification;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Doctrine\DBAL\Schema\Schema;

/**
 * Auto-generated Migration: Please modify to your needs!
 */
class Version20180123103147 extends AbstractMigration implements ContainerAwareInterface
{
    private $container;

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

    /**
     * @param Schema $schema
     */
    public function up(Schema $schema)
    {
        $em = $this->container->getDoctrine()->getManager();
        $tableName = $em->getClassMetadata('AppBundle:PushNotification')->getTableName();

        $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');

        $this->addSql('ALTER TABLE '.$tableName.' ADD COLUMN   last_offset int(11) ');
        $this->addSql('ALTER TABLE '.$tableName.' ADD COLUMN   failure_count int(11) ');
        $this->addSql('ALTER TABLE '.$tableName.' ADD COLUMN   is_sent int(11) ');
        $this->addSql('ALTER TABLE '.$tableName.' ADD COLUMN   send_date datetime ');


    }

    /**
     * @param Schema $schema
     */
    public function down(Schema $schema)
    {

        $em = $this->container->getDoctrine()->getManager();
        $tableName = $em->getClassMetadata('AppBundle:PushNotification')->getTableName();

        $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');

        $this->addSql('ALTER TABLE '.$tableName.' DROP COLUMN   last_offset ');
        $this->addSql('ALTER TABLE '.$tableName.' DROP COLUMN   failure_count ');
        $this->addSql('ALTER TABLE '.$tableName.' DROP COLUMN   is_sent ');
        $this->addSql('ALTER TABLE '.$tableName.' DROP COLUMN   send_date ');

    }
}
$this->addSql('ALTER TABLE push_notification ADD COLUMN  last_offset int(11)');

似乎您正试图将
容器
注入到另一个服务中,这通常是非常不鼓励的。相反,您可以插入
EntityManagerInterface
并使用它

但是,我确实看到这是一个迁移文件。在您给定的代码中,没有任何理由说明为什么会出现这种情况,而只是将表名硬编码如下:

<?php

namespace Application\Migrations;

use Doctrine\ORM\Mapping\PushNotification;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Doctrine\DBAL\Schema\Schema;

/**
 * Auto-generated Migration: Please modify to your needs!
 */
class Version20180123103147 extends AbstractMigration implements ContainerAwareInterface
{
    private $container;

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

    /**
     * @param Schema $schema
     */
    public function up(Schema $schema)
    {
        $em = $this->container->getDoctrine()->getManager();
        $tableName = $em->getClassMetadata('AppBundle:PushNotification')->getTableName();

        $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');

        $this->addSql('ALTER TABLE '.$tableName.' ADD COLUMN   last_offset int(11) ');
        $this->addSql('ALTER TABLE '.$tableName.' ADD COLUMN   failure_count int(11) ');
        $this->addSql('ALTER TABLE '.$tableName.' ADD COLUMN   is_sent int(11) ');
        $this->addSql('ALTER TABLE '.$tableName.' ADD COLUMN   send_date datetime ');


    }

    /**
     * @param Schema $schema
     */
    public function down(Schema $schema)
    {

        $em = $this->container->getDoctrine()->getManager();
        $tableName = $em->getClassMetadata('AppBundle:PushNotification')->getTableName();

        $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');

        $this->addSql('ALTER TABLE '.$tableName.' DROP COLUMN   last_offset ');
        $this->addSql('ALTER TABLE '.$tableName.' DROP COLUMN   failure_count ');
        $this->addSql('ALTER TABLE '.$tableName.' DROP COLUMN   is_sent ');
        $this->addSql('ALTER TABLE '.$tableName.' DROP COLUMN   send_date ');

    }
}
$this->addSql('ALTER TABLE push_notification ADD COLUMN  last_offset int(11)');
因此,您收到该错误的原因是,迁移希望将容器的实例传递给您的迁移,但是,由于您没有将迁移注册为服务,因此这种情况从未发生


(旁注:永远不要将迁移注册为服务)

这里有好的地方。注入EntityManagerInterface不也包括将迁移注册为服务吗?正如总结中所解释的,是的。啊,我明白你的意思了。谢谢。如果问题已经解决,请随时将其标记为“已解决”:)您不需要将迁移声明为服务,正如所解释的,错误是抱怨方法签名,从我所看到的情况来看,您没有导入
ContainerInterface
,因此类型不匹配