【问题标题】:Execute sql script when updating database schema Doctrine更新数据库模式 Doctrine 时执行 sql 脚本
【发布时间】:2016-04-15 13:54:00
【问题描述】:

执行时是否可以附加(或执行)自定义sql查询:

app/console doctrine:schema:update --force

我有一个创建所有视图的脚本,我希望在更新数据库架构时更新它们。

【问题讨论】:

  • 您可以在 symfony 上创建自定义控制台的命令:link
  • 有没有办法扩展学说命令?
  • 看看这个link

标签: sql symfony doctrine-orm


【解决方案1】:

当然,您可以扩展UpdateSchemaCommand 命令并将EntityManager 注入defining the command as a service

命令:

// src/AppBundle/Command/CustomUpdateSchemaCommand.php
<?php

namespace AppBundle\Command;

use Doctrine\Bundle\DoctrineBundle\Command\Proxy\UpdateSchemaDoctrineCommand;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;

class CustomUpdateSchemaCommand extends UpdateSchemaDoctrineCommand
{
    /** @var EntityManagerInterface */
    private $em;

    /**
     * @param EntityManagerInterface $em
     */
    public function __construct(EntityManagerInterface $em)
    {
        $this->em = $em;

        parent::__construct();
    }

    /**
     * {@inheritDoc}
     */
    protected function configure()
    {
        parent::configure();
    }

    /**
     * {@inheritDoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('Hello world');
        $conn = $this->em->getConnection();
        $conn->exec(/* QUERY */);


        return parent::execute($input, $output);
    }
}

服务:

// app/config/services.yml
app.command.custom_schema_update_command:
    class: App\SportBundle\Command\CustomUpdateSchemaCommand
    arguments: ["@doctrine.orm.entity_manager"]
    tags:
        -  { name: console.command }

希望这会有所帮助。

【讨论】:

  • 不客气@MNIFAKRAM。以防万一,为什么您不投票赞成答案?您是否认为答案中没有某些内容或可能会更好?有时 SO 初学者只是忘了投票。
  • 对不起,我忘记了。
猜你喜欢
  • 2011-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-13
  • 2014-08-29
  • 1970-01-01
相关资源
最近更新 更多