Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

deleteentitydatacommand

  • Loading branch information...
commit f1641c0a4f610e08d8480898e9470c62c7c81df6 1 parent 98cfc88
@Mparaiso authored
View
51 src/Mparaiso/Doctrine/ORM/Command/DeleteEntityDataCommand.php
@@ -0,0 +1,51 @@
+<?php
+namespace Mparaiso\Doctrine\ORM\Command;
+
+
+use Symfony\Component\Console\Command\Command;
+use Doctrine\ORM\Mapping\ClassMetadata;
+use Doctrine\ORM\EntityManager;
+use Doctrine\Common\Persistence\AbstractManagerRegistry;
+use Symfony\Component\Console\Helper\DialogHelper;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Input\InputInterface;
+
+class DeleteEntityDataCommand extends Command
+{
+ protected function configure()
+ {
+ parent::configure();
+ $this->setName("doctrine:delete-managed-datas")
+ ->setDescription('Remove all managed entity datas from the database');
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ parent::execute($input, $output);
+ $input->setInteractive(TRUE);
+ $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");
+ $registry = $app['orm.manager_registry'];
+ /* @var $registry AbstractManagerRegistry */
+ $managers = $registry->getManagers();
+ foreach ($managers as $manager) {
+ /* @var $manager EntityManager */
+ $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);
+ }
+ }
+ $manager->flush();
+ }
+ }
+
+}
View
3  src/Mparaiso/Provider/DoctrineORMServiceProvider.php
@@ -3,6 +3,8 @@
namespace Mparaiso\Provider;
use Silex\ServiceProviderInterface;
+use Mparaiso\Doctrine\ORM\Command\DeleteEntityDataCommand;
+
use Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator;
use Exception;
use Mparaiso\Doctrine\ORM\DoctrineManagerRegistry;
@@ -141,6 +143,7 @@ public function register(Application $app)
ConsoleRunner::addCommands($app["console"]);
$console->add(new ImportMappingDoctrineCommand);
$console->add(new LoadFixturesCommand);
+ $console->add(new DeleteEntityDataCommand);
}
});
}
Please sign in to comment.
Something went wrong with that request. Please try again.