Last active
August 29, 2015 14:24
-
-
Save kuldipem/00c1a4db146e0a2a4b32 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Kem\CoreBundle\Command; | |
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
class DropEntityTableCommand extends ContainerAwareCommand | |
{ | |
protected function configure() | |
{ | |
$this | |
->setName('kem:entity:table:drop') | |
->setDescription('Drop table of the entity') | |
->setDefinition(array( | |
new InputArgument('entity-name', InputArgument::REQUIRED, 'The Entity name'), | |
new InputOption('foreign-key-check', null, InputOption::VALUE_NONE, 'Disable foregin key check disable.'), | |
new InputOption('force', null, InputOption::VALUE_NONE, 'Force to drop table, by disableing foregin key check.'), | |
)); | |
} | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$entityName = $input->getArgument('entity-name'); | |
$foreignKeyCheck = $input->getOption('foreign-key-check'); | |
$force = $input->getOption('force'); | |
$em = $this->getContainer()->get('doctrine.orm.entity_manager'); | |
$tableName = $em->getClassMetadata($entityName)->getTableName(); | |
$query=""; | |
if ($foreignKeyCheck || $force) { | |
$query.="SET FOREIGN_KEY_CHECKS=0;"; // disable foregin key check | |
} | |
$query .= "DROP table $tableName;"; | |
if ($foreignKeyCheck || $force) { | |
$query.="SET FOREIGN_KEY_CHECKS=1;"; // enable again | |
} | |
$em->getConnection()->exec($query); | |
$output->writeln( | |
sprintf( | |
'Table drop with name <info>%s</info>, of Entity <info>%s</info>', | |
$tableName, | |
$entityName | |
) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment