DoctrineCommand.php 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /*
  3. * This file is part of the Doctrine Bundle
  4. *
  5. * The code was originally distributed inside the Symfony framework.
  6. *
  7. * (c) Fabien Potencier <fabien@symfony.com>
  8. * (c) Doctrine Project, Benjamin Eberlei <kontakt@beberlei.de>
  9. *
  10. * For the full copyright and license information, please view the LICENSE
  11. * file that was distributed with this source code.
  12. */
  13. namespace Doctrine\Bundle\DoctrineBundle\Command;
  14. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  15. use Doctrine\ORM\Tools\EntityGenerator;
  16. /**
  17. * Base class for Doctrine console commands to extend from.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. */
  21. abstract class DoctrineCommand extends ContainerAwareCommand
  22. {
  23. /**
  24. * get a doctrine entity generator
  25. *
  26. * @return EntityGenerator
  27. */
  28. protected function getEntityGenerator()
  29. {
  30. $entityGenerator = new EntityGenerator();
  31. $entityGenerator->setGenerateAnnotations(false);
  32. $entityGenerator->setGenerateStubMethods(true);
  33. $entityGenerator->setRegenerateEntityIfExists(false);
  34. $entityGenerator->setUpdateEntityIfExists(true);
  35. $entityGenerator->setNumSpaces(4);
  36. $entityGenerator->setAnnotationPrefix('ORM\\');
  37. return $entityGenerator;
  38. }
  39. /**
  40. * Get a doctrine entity manager by symfony name.
  41. *
  42. * @param string $name
  43. * @return \Doctrine\ORM\EntityManager
  44. */
  45. protected function getEntityManager($name)
  46. {
  47. return $this->getContainer()->get('doctrine')->getManager($name);
  48. }
  49. /**
  50. * Get a doctrine dbal connection by symfony name.
  51. *
  52. * @param string $name
  53. * @return \Doctrine\DBAL\Connection
  54. */
  55. protected function getDoctrineConnection($name)
  56. {
  57. return $this->getContainer()->get('doctrine')->getConnection($name);
  58. }
  59. }