Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
426 views
in Technique[技术] by (71.8m points)

doctrine orm - Ignore a Doctrine2 Entity when running schema-manager update

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Based on the original alswer of ChrisR inspired in Marco Pivetta's post I'm adding here the solution if you're using Symfony2:

Looks like Symfony2 doesn't use the original Doctrine command at: DoctrineORMToolsConsoleCommandSchemaToolUpdateCommand

Instead it uses the one in the bundle: DoctrineBundleDoctrineBundleCommandProxyUpdateSchemaDoctrineCommand

So basically that is the class that must be extended, ending up in having:

src/Acme/CoreBundle/Command/DoctrineUpdateCommand.php:

<?php

namespace AcmeCoreBundleCommand;

use SymfonyComponentConsoleInputInputOption;
use SymfonyComponentConsoleInputInputArgument;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
use DoctrineORMToolsSchemaTool;

class DoctrineUpdateCommand extends DoctrineBundleDoctrineBundleCommandProxyUpdateSchemaDoctrineCommand {

  protected $ignoredEntities = array(
      'AcmeCoreBundleEntityEntityToIgnore'
  );

  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);
      }
    }

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...