3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\ORM\Tools\Console\Command;
22 use Symfony\Component\Console\Input\InputArgument,
23 Symfony\Component\Console\Input\InputOption,
24 Symfony\Component\Console;
27 * Command to execute DQL queries in a given EntityManager.
30 * @link www.doctrine-project.org
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>
37 class RunDqlCommand extends Console\Command\Command
40 * @see Console\Command\Command
42 protected function configure()
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.'),
50 'hydrate', null, InputOption::VALUE_REQUIRED,
51 'Hydration mode of result set. Should be either: object, array, scalar or single-scalar.',
55 'first-result', null, InputOption::VALUE_REQUIRED,
56 'The first result in the result set.'
59 'max-result', null, InputOption::VALUE_REQUIRED,
60 'The maximum number of results in the result set.'
63 'depth', null, InputOption::VALUE_REQUIRED,
64 'Dumping depth of Entity graph.', 7
68 Executes arbitrary DQL directly from the command line.
74 * @see Console\Command\Command
76 protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
78 $em = $this->getHelper('em')->getEntityManager();
80 if (($dql = $input->getArgument('dql')) === null) {
81 throw new \RuntimeException("Argument 'DQL' is required in order to execute this command correctly.");
84 $depth = $input->getOption('depth');
86 if ( ! is_numeric($depth)) {
87 throw new \LogicException("Option 'depth' must contains an integer value");
90 $hydrationModeName = $input->getOption('hydrate');
91 $hydrationMode = 'Doctrine\ORM\Query::HYDRATE_' . strtoupper(str_replace('-', '_', $hydrationModeName));
93 if ( ! defined($hydrationMode)) {
94 throw new \RuntimeException(
95 "Hydration mode '$hydrationModeName' does not exist. It should be either: object. array, scalar or single-scalar."
99 $query = $em->createQuery($dql);
101 if (($firstResult = $input->getOption('first-result')) !== null) {
102 if ( ! is_numeric($firstResult)) {
103 throw new \LogicException("Option 'first-result' must contains an integer value");
106 $query->setFirstResult((int) $firstResult);
109 if (($maxResult = $input->getOption('max-result')) !== null) {
110 if ( ! is_numeric($maxResult)) {
111 throw new \LogicException("Option 'max-result' must contains an integer value");
114 $query->setMaxResults((int) $maxResult);
117 $resultSet = $query->execute(array(), constant($hydrationMode));
119 \Doctrine\Common\Util\Debug::dump($resultSet, $input->getOption('depth'));