RunDqlCommand.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Doctrine\ORM\Tools\Console\Command;
  22. use Symfony\Component\Console\Input\InputArgument,
  23. Symfony\Component\Console\Input\InputOption,
  24. Symfony\Component\Console;
  25. /**
  26. * Command to execute DQL queries in a given EntityManager.
  27. *
  28. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  29. * @link www.doctrine-project.org
  30. * @since 2.0
  31. * @version $Revision$
  32. * @author Benjamin Eberlei <kontakt@beberlei.de>
  33. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  34. * @author Jonathan Wage <jonwage@gmail.com>
  35. * @author Roman Borschel <roman@code-factory.org>
  36. */
  37. class RunDqlCommand extends Console\Command\Command
  38. {
  39. /**
  40. * @see Console\Command\Command
  41. */
  42. protected function configure()
  43. {
  44. $this
  45. ->setName('orm:run-dql')
  46. ->setDescription('Executes arbitrary DQL directly from the command line.')
  47. ->setDefinition(array(
  48. new InputArgument('dql', InputArgument::REQUIRED, 'The DQL to execute.'),
  49. new InputOption(
  50. 'hydrate', null, InputOption::VALUE_REQUIRED,
  51. 'Hydration mode of result set. Should be either: object, array, scalar or single-scalar.',
  52. 'object'
  53. ),
  54. new InputOption(
  55. 'first-result', null, InputOption::VALUE_REQUIRED,
  56. 'The first result in the result set.'
  57. ),
  58. new InputOption(
  59. 'max-result', null, InputOption::VALUE_REQUIRED,
  60. 'The maximum number of results in the result set.'
  61. ),
  62. new InputOption(
  63. 'depth', null, InputOption::VALUE_REQUIRED,
  64. 'Dumping depth of Entity graph.', 7
  65. )
  66. ))
  67. ->setHelp(<<<EOT
  68. Executes arbitrary DQL directly from the command line.
  69. EOT
  70. );
  71. }
  72. /**
  73. * @see Console\Command\Command
  74. */
  75. protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
  76. {
  77. $em = $this->getHelper('em')->getEntityManager();
  78. if (($dql = $input->getArgument('dql')) === null) {
  79. throw new \RuntimeException("Argument 'DQL' is required in order to execute this command correctly.");
  80. }
  81. $depth = $input->getOption('depth');
  82. if ( ! is_numeric($depth)) {
  83. throw new \LogicException("Option 'depth' must contains an integer value");
  84. }
  85. $hydrationModeName = $input->getOption('hydrate');
  86. $hydrationMode = 'Doctrine\ORM\Query::HYDRATE_' . strtoupper(str_replace('-', '_', $hydrationModeName));
  87. if ( ! defined($hydrationMode)) {
  88. throw new \RuntimeException(
  89. "Hydration mode '$hydrationModeName' does not exist. It should be either: object. array, scalar or single-scalar."
  90. );
  91. }
  92. $query = $em->createQuery($dql);
  93. if (($firstResult = $input->getOption('first-result')) !== null) {
  94. if ( ! is_numeric($firstResult)) {
  95. throw new \LogicException("Option 'first-result' must contains an integer value");
  96. }
  97. $query->setFirstResult((int) $firstResult);
  98. }
  99. if (($maxResult = $input->getOption('max-result')) !== null) {
  100. if ( ! is_numeric($maxResult)) {
  101. throw new \LogicException("Option 'max-result' must contains an integer value");
  102. }
  103. $query->setMaxResults((int) $maxResult);
  104. }
  105. $resultSet = $query->execute(array(), constant($hydrationMode));
  106. \Doctrine\Common\Util\Debug::dump($resultSet, $input->getOption('depth'));
  107. }
  108. }