DoctrineCommandHelper.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace Doctrine\Bundle\DoctrineBundle\Command\Proxy;
  3. use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
  6. use Symfony\Bundle\FrameworkBundle\Console\Application;
  7. use function assert;
  8. /**
  9. * Provides some helper and convenience methods to configure doctrine commands in the context of bundles
  10. * and multiple connections/entity managers.
  11. */
  12. abstract class DoctrineCommandHelper
  13. {
  14. /**
  15. * Convenience method to push the helper sets of a given entity manager into the application.
  16. *
  17. * @param string $emName
  18. */
  19. public static function setApplicationEntityManager(Application $application, $emName)
  20. {
  21. $em = $application->getKernel()->getContainer()->get('doctrine')->getManager($emName);
  22. assert($em instanceof EntityManagerInterface);
  23. $helperSet = $application->getHelperSet();
  24. $helperSet->set(new ConnectionHelper($em->getConnection()), 'db');
  25. $helperSet->set(new EntityManagerHelper($em), 'em');
  26. }
  27. /**
  28. * Convenience method to push the helper sets of a given connection into the application.
  29. *
  30. * @param string $connName
  31. */
  32. public static function setApplicationConnection(Application $application, $connName)
  33. {
  34. $connection = $application->getKernel()->getContainer()->get('doctrine')->getConnection($connName);
  35. $helperSet = $application->getHelperSet();
  36. $helperSet->set(new ConnectionHelper($connection), 'db');
  37. }
  38. }