Ignore a Doctrine2 Entity when running schema-manager update

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:
\Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand

Instead it uses the one in the bundle:
\Doctrine\Bundle\DoctrineBundle\Command\Proxy\UpdateSchemaDoctrineCommand

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

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

<?php

namespace App\Command;

use Doctrine\Bundle\DoctrineBundle\Command\Proxy\UpdateSchemaDoctrineCommand;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Tools\SchemaTool;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class DoctrineUpdateCommand extends UpdateSchemaDoctrineCommand
{
    protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas, SymfonyStyle $ui): ?int
    {
        $ignoredEntities = [
            'App\Entity\EntityToIgnore',
        ];
        $metadatas = array_filter($metadatas, static function (ClassMetadata $classMetadata) use ($ignoredEntities) {
            return !in_array($classMetadata->getName(), $ignoredEntities, true);
        });
        return parent::executeSchemaCommand($input, $output, $schemaTool, $metadatas, $ui);
    }
}

Leave a Comment