我有一个如下所示的Doctrine迁移:
<?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 ');
}
}
...当运行迁移时,我收到一条错误,指出Declaration of Application\Migrations\Version20180123103147::setContainer() must be compatible with Symfony\Component\DependencyInjection\ContainerAwareInterface::setContainer(Symfony\Component\DependencyInjection\ContainerInterface $container = NULL)
这个错误似乎不合适,因为我直接从相关接口复制了方法签名。
我在这里缺少一些明显的代码吗?
答案 0 :(得分:1)
您似乎正在尝试将container
注入其他服务,这通常非常气馁。相反,您可以注入EntityManagerInterface
并使用它。
但是,我确实看到这是一个迁移文件。你的代码中没有理由为什么这应该在这里,而只是像这样对表名进行硬编码:
$this->addSql('ALTER TABLE push_notification ADD COLUMN last_offset int(11)');
总结一下,您收到该错误的原因是因为迁移期望将容器的实例传递给您的迁移,但是,由于您未将迁移注册为服务,因此不会发生这种情况。
(sidenote: never register a migration as a service)