MigrationUpgradeCommand.php 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace Muzich\CoreBundle\Command;
  3. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  4. use Symfony\Component\Console\Input\InputArgument;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Input\InputOption;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. //use Muzich\CoreBundle\Managers\CommentsManager;
  9. class MigrationUpgradeCommand extends ContainerAwareCommand
  10. {
  11. protected function configure()
  12. {
  13. $this
  14. ->setName('migration:upgrade')
  15. ->setDescription('Mise a jour de données nécéssaire pour migrations')
  16. ->addArgument('from', InputArgument::REQUIRED, 'version de départ')
  17. ->addArgument('to', InputArgument::REQUIRED, 'version visé')
  18. ;
  19. }
  20. protected function execute(InputInterface $input, OutputInterface $output)
  21. {
  22. $doctrine = $this->getContainer()->get('doctrine');
  23. $em = $doctrine->getEntityManager();
  24. $output->writeln('#');
  25. $output->writeln('## Outil de migration ##');
  26. $output->writeln('#');
  27. $proceeded = false;
  28. if ($input->getArgument('from') == '0.6' && $input->getArgument('to') == '0.7')
  29. {
  30. $proceeded = true;
  31. $output->writeln('<info>Mise a jour des données "commentaires" pour migration 0.6 => 0.7</info>');
  32. /**
  33. * Pour cette migration on a besoin de rajouter une valeur aux données
  34. * de commentaire d'éléments.
  35. */
  36. $elements = $em->getRepository('MuzichCoreBundle:Element')->findAll();
  37. foreach ($elements as $element)
  38. {
  39. if (count(($comments = $element->getComments())))
  40. {
  41. // Oui un for serai plus performant ...
  42. foreach ($comments as $i => $comment)
  43. {
  44. // Si il n'y as pas d'enregistrement 'a' dans le commentaire
  45. if (!array_key_exists('a', $comment))
  46. {
  47. // Il faut le rajouter
  48. $comments[$i]['a'] = array();
  49. }
  50. }
  51. $element->setComments($comments);
  52. $em->persist($element);
  53. }
  54. }
  55. $em->flush();
  56. $output->writeln('<info>Terminé !</info>');
  57. }
  58. if (!$proceeded)
  59. {
  60. $output->writeln('<error>Versions saisies non prises en charges</error>');
  61. }
  62. }
  63. }