本文介绍了在运行模式管理器更新时忽略Doctrine2实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定义的Doctrine实体映射到数据库中的View。一切工作正常,实体关系按预期工作正常。

I've got a Doctrine Entity defined that maps to a View in my database. All works fine, the Entity relations work fine as expected.

现在问题是,在CLI上运行 orm:schema-manager:update 时,会为此创建一个表实体是我想要防止的。这个实体已经有了一个视图,不需要为它创建一个表。

Problem now is that when running orm:schema-manager:update on the CLI a table gets created for this entity which is something I want to prevent. There already is a view for this Entity, no need to create a table for it.

我可以注释实体,以便在保持不变的时候不会创建一个表访问所有实体相关功能(关联,...)?

Can I annotate the Entity so that a table won't be created while still keeping access to all Entity related functionality (associations, ...)?

推荐答案

最终,这是相当简单的,我只需要 \Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand 到我自己的CLI命令。在该子类中过滤 $ metadatas 数组,传递给 executeSchemaCommand(),然后将其传递给父函数。

Eventually it was fairly simple, I just had to subclass the \Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand into my own CLI Command. In that subclass filter the $metadatas array that's being passed to executeSchemaCommand() and then pass it on to the parent function.

只需将这个新的子类命令附加到您在您的教义cli脚本中使用的ConsoleApplication并完成!

Just attach this new subclassed command to the ConsoleApplication you are using in your doctrine cli script and done!

以下是扩展命令,在生产中,您可能希望从配置或东西中获取 $ ignoredEntities 属性,这应该让您在途中。

Below is the extended command, in production you'll probably want to fetch the $ignoredEntities property from you config or something, this should put you on the way.

<?php
use Symfony\Component\Console\Input\InputArgument,
    Symfony\Component\Console\Input\InputOption,
    Symfony\Component\Console\Input\InputInterface,
    Symfony\Component\Console\Output\OutputInterface,
    Doctrine\ORM\Tools\SchemaTool;

class My_Doctrine_Tools_UpdateCommand extends \Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand
{
    protected $name = 'orm:schema-tool:myupdate';

    protected $ignoredEntities = array(
        'Entity\Asset\Name'
    );

    protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas)
    {
        /** @var $metadata \Doctrine\ORM\Mapping\ClassMetadata */
        $newMetadatas = array();
        foreach($metadatas as $metadata) {
            if(!in_array($metadata->getName(), $this->ignoredEntities)){
                array_push($newMetadatas, $metadata);
            }
        }

        return parent::executeSchemaCommand($input, $output, $schemaTool, $newMetadatas);
    }
}

PS:信用转到Marco Pivetta让我正确的轨道。

PS: credits go to Marco Pivetta for putting me on the right track. https://groups.google.com/forum/?fromgroups=#!topic/doctrine-user/rwWXZ7faPsA

这篇关于在运行模式管理器更新时忽略Doctrine2实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:17