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

问题描述

我定义了一个 Doctrine 实体,它映射到我的数据库中的一个视图.一切正常,实体关系按预期正常工作.

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, ...)?

推荐答案

最终还是很简单的,我只需要对 DoctrineORMToolsConsoleCommandSchemaToolUpdateCommand 进行子类化进入我自己的 CLI 命令.在该子类中过滤 $metadatas 数组,该数组将传递给 executeSchemaCommand(),然后将其传递给父函数.

Eventually it was fairly simple, I just had to subclass the DoctrineORMToolsConsoleCommandSchemaToolUpdateCommand 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 SymfonyComponentConsoleInputInputArgument,
    SymfonyComponentConsoleInputInputOption,
    SymfonyComponentConsoleInputInputInterface,
    SymfonyComponentConsoleOutputOutputInterface,
    DoctrineORMToolsSchemaTool;

class My_Doctrine_Tools_UpdateCommand extends DoctrineORMToolsConsoleCommandSchemaToolUpdateCommand
{
    protected $name = 'orm:schema-tool:myupdate';

    protected $ignoredEntities = array(
        'EntityAssetName'
    );

    protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas)
    {
        /** @var $metadata DoctrineORMMappingClassMetadata */
        $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 让我走上正轨.https://groups.google.com/forum/?fromgroups=#!topic/doctrine-user/rwWXZ7faPsA

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