Permalink
Browse files

"deleteentitydatacommand filter option added"

  • Loading branch information...
1 parent f1641c0 commit f5146c52beb3f791ac28eff80ccc615932cf5e6d mparaiso committed May 1, 2013
Showing with 13 additions and 6 deletions.
  1. +13 −6 src/Mparaiso/Doctrine/ORM/Command/DeleteEntityDataCommand.php
@@ -3,6 +3,7 @@
use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputOption;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\EntityManager;
use Doctrine\Common\Persistence\AbstractManagerRegistry;
@@ -16,20 +17,21 @@ protected function configure()
{
parent::configure();
$this->setName("doctrine:delete-managed-datas")
+ ->addOption("filter", "f", InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, "filter by entity name", array())
->setDescription('Remove all managed entity datas from the database');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
- parent::execute($input, $output);
$input->setInteractive(TRUE);
+ $filter = $input->getOption('filter');
$dialog = $this->getHelper("dialog");
/* @var $dialog DialogHelper */
#@note @silex commande interactive
if (!$dialog->askConfirmation($output, "Confirm database managed entities deletion(yes|no):")) {
return 1;
}
- $app = $this->getHelper("app");
+ $app = $this->getHelper("app")->getApplication();
$registry = $app['orm.manager_registry'];
/* @var $registry AbstractManagerRegistry */
$managers = $registry->getManagers();
@@ -38,10 +40,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
$metadatas = $manager->getMetadataFactory()->getAllMetadata();
foreach ($metadatas as $metadata) {
/* @var $metadata ClassMetadata */
- $class = $metadata->getName();
- $entities = $manager->getRepository($class)->findAll();
- foreach ($entities as $entity) {
- $manager->remove($entity);
+ if (!$metadata->isMappedSuperclass) {
+ $class = $metadata->getName();
+ if ($filter == NULL || in_array($class, $filter)) {
+ $entities = $manager->getRepository($class)->findAll();
+ foreach ($entities as $entity) {
+ $manager->remove($entity);
+ }
+ $output->writeln($class . " datas have been removed.");
+ }
}
}
$manager->flush();

0 comments on commit f5146c5

Please sign in to comment.