source: pro-violet-viettel/sourcecode/application/libraries/Doctrine/ORM/Tools/Console/Command/RunDqlCommand.php @ 345

Last change on this file since 345 was 345, checked in by quyenla, 11 years ago

collaborator page

File size: 4.5 KB
Line 
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
22namespace Doctrine\ORM\Tools\Console\Command;
23
24use Symfony\Component\Console\Input\InputArgument,
25    Symfony\Component\Console\Input\InputOption,
26    Symfony\Component\Console;
27
28/**
29 * Command to execute DQL queries in a given EntityManager.
30 *
31 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
32 * @link    www.doctrine-project.org
33 * @since   2.0
34 * @version $Revision$
35 * @author  Benjamin Eberlei <kontakt@beberlei.de>
36 * @author  Guilherme Blanco <guilhermeblanco@hotmail.com>
37 * @author  Jonathan Wage <jonwage@gmail.com>
38 * @author  Roman Borschel <roman@code-factory.org>
39 */
40class RunDqlCommand extends Console\Command\Command
41{
42    /**
43     * @see Console\Command\Command
44     */
45    protected function configure()
46    {
47        $this
48        ->setName('orm:run-dql')
49        ->setDescription('Executes arbitrary DQL directly from the command line.')
50        ->setDefinition(array(
51            new InputArgument('dql', InputArgument::REQUIRED, 'The DQL to execute.'),
52            new InputOption(
53                'hydrate', null, InputOption::VALUE_REQUIRED,
54                'Hydration mode of result set. Should be either: object, array, scalar or single-scalar.',
55                'object'
56            ),
57            new InputOption(
58                'first-result', null, InputOption::VALUE_REQUIRED,
59                'The first result in the result set.'
60            ),
61            new InputOption(
62                'max-result', null, InputOption::VALUE_REQUIRED,
63                'The maximum number of results in the result set.'
64            ),
65            new InputOption(
66                'depth', null, InputOption::VALUE_REQUIRED,
67                'Dumping depth of Entity graph.', 7
68            )
69        ))
70        ->setHelp(<<<EOT
71Executes arbitrary DQL directly from the command line.
72EOT
73        );
74    }
75
76    /**
77     * @see Console\Command\Command
78     */
79    protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
80    {
81        $em = $this->getHelper('em')->getEntityManager();
82
83        if (($dql = $input->getArgument('dql')) === null) {
84            throw new \RuntimeException("Argument 'DQL' is required in order to execute this command correctly.");
85        }
86
87        $depth = $input->getOption('depth');
88
89        if ( ! is_numeric($depth)) {
90            throw new \LogicException("Option 'depth' must contains an integer value");
91        }
92
93        $hydrationModeName = $input->getOption('hydrate');
94        $hydrationMode = 'Doctrine\ORM\Query::HYDRATE_' . strtoupper(str_replace('-', '_', $hydrationModeName));
95
96        if ( ! defined($hydrationMode)) {
97            throw new \RuntimeException(
98                "Hydration mode '$hydrationModeName' does not exist. It should be either: object. array, scalar or single-scalar."
99            );
100        }
101
102        $query = $em->createQuery($dql);
103
104        if (($firstResult = $input->getOption('first-result')) !== null) {
105            if ( ! is_numeric($firstResult)) {
106                throw new \LogicException("Option 'first-result' must contains an integer value");
107            }
108
109            $query->setFirstResult((int) $firstResult);
110        }
111
112        if (($maxResult = $input->getOption('max-result')) !== null) {
113            if ( ! is_numeric($maxResult)) {
114                throw new \LogicException("Option 'max-result' must contains an integer value");
115            }
116
117            $query->setMaxResults((int) $maxResult);
118        }
119
120        $resultSet = $query->execute(array(), constant($hydrationMode));
121
122        \Doctrine\Common\Util\Debug::dump($resultSet, $input->getOption('depth'));
123    }
124}
Note: See TracBrowser for help on using the repository browser.