Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / lib / Doctrine / ORM / Tools / Console / Command / RunDqlCommand.php
1 <?php
2 /*
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.
14  *
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>.
18  */
19
20 namespace Doctrine\ORM\Tools\Console\Command;
21
22 use Symfony\Component\Console\Input\InputArgument,
23     Symfony\Component\Console\Input\InputOption,
24     Symfony\Component\Console;
25
26 /**
27  * Command to execute DQL queries in a given EntityManager.
28  *
29  * 
30  * @link    www.doctrine-project.org
31  * @since   2.0
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     /**
74      * @see Console\Command\Command
75      */
76     protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
77     {
78         $em = $this->getHelper('em')->getEntityManager();
79
80         if (($dql = $input->getArgument('dql')) === null) {
81             throw new \RuntimeException("Argument 'DQL' is required in order to execute this command correctly.");
82         }
83
84         $depth = $input->getOption('depth');
85
86         if ( ! is_numeric($depth)) {
87             throw new \LogicException("Option 'depth' must contains an integer value");
88         }
89
90         $hydrationModeName = $input->getOption('hydrate');
91         $hydrationMode = 'Doctrine\ORM\Query::HYDRATE_' . strtoupper(str_replace('-', '_', $hydrationModeName));
92
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."
96             );
97         }
98
99         $query = $em->createQuery($dql);
100
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");
104             }
105
106             $query->setFirstResult((int) $firstResult);
107         }
108
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");
112             }
113
114             $query->setMaxResults((int) $maxResult);
115         }
116
117         $resultSet = $query->execute(array(), constant($hydrationMode));
118
119         \Doctrine\Common\Util\Debug::dump($resultSet, $input->getOption('depth'));
120     }
121 }