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

app/console doctrine:schema:update --force 

我有一个脚本可以创建我所有的 View ,我希望它们在我更新数据库架构时得到更新。

请您参考如下方法:

当然,您可以扩展 UpdateSchemaCommand命令并将 EntityManager 注入(inject) 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 } 

希望这对您有所帮助。


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!