Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sylvainfilteau/5178173 to your computer and use it in GitHub Desktop.
Save sylvainfilteau/5178173 to your computer and use it in GitHub Desktop.
Replace the orm:schema-tool:update command from Doctrine 2 with UpdateCommand.php. It will allow you to ignore table starting with B_ (see bootstrap file)
// Set this Doctrine 2 connection configuration to ignore view starting with B_
$config->setFilterSchemaAssetsExpression("/^B_/i");
<?php
namespace Comet\Event\Subscriber;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Tools\ToolEvents;
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
class RemoveFilteredTableEventSubscriber implements EventSubscriber {
private $filter_schema_assets_expression;
public function __construct($filter_schema_assets_expression) {
$this->filter_schema_assets_expression = $filter_schema_assets_expression;
}
public function postGenerateSchema(GenerateSchemaEventArgs $args) {
$schema = $args->getSchema();
$table_names = $schema->getTableNames();
foreach ($table_names as $table_name) {
$table_name = substr($table_name, strpos($table_name, '.') + 1);
if (!preg_match($this->filter_schema_assets_expression, $table_name)) {
$schema->dropTable($table_name);
}
}
}
public function getSubscribedEvents() {
return array(ToolEvents::postGenerateSchema);
}
}
<?php
namespace Comet\Command\SchemaTool;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand as DoctrineUpdateCommand;
/**
* Command that overrides the orm:schema-tool:update to ignore tables
*
* This override will look for the annotation
* @Comet\Annotation\IgnoreSchemaUpdate in entity definition. If it's there,
* the entity will not be added to the metadata to update
*/
class UpdateCommand extends DoctrineUpdateCommand {
protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas) {
$em_helper = $this->getHelper('em');
/* @var $em \Doctrine\ORM\EntityManager */
$em = $em_helper->getEntityManager();
$event_manager = $em->getEventManager();
$conn = $em->getConnection();
if ($filter_expression = $conn->getConfiguration()->getFilterSchemaAssetsExpression()) {
$event_manager->addEventSubscriber(new \Comet\Event\Subscriber\RemoveFilteredTableEventSubscriber($filter_expression));
}
parent::executeSchemaCommand($input, $output, $schemaTool, $new_metadatas);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment