Development

/branches/1.2/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineDqlTask.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineDqlTask.class.php

Revision 14213, 2.5 kB (checked in by Jonathan.Wage, 6 years ago)

[1.2] sfDoctrinePlugin: fixes issue with application option in tasks (closes #5238)

  • Property svn:keywords set to Id Date Author Revision HeadURL
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  * (c) Jonathan H. Wage <jonwage@gmail.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 require_once(dirname(__FILE__).'/sfDoctrineBaseTask.class.php');
13
14 /**
15  * Creates database for current model.
16  *
17  * @package    symfony
18  * @subpackage doctrine
19  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
20  * @author     Jonathan H. Wage <jonwage@gmail.com>
21  * @version    SVN: $Id$
22  */
23 class sfDoctrineDqlTask extends sfDoctrineBaseTask
24 {
25   /**
26    * @see sfTask
27    */
28   protected function configure()
29   {
30     $this->addArguments(array(
31       new sfCommandArgument('dql_query', sfCommandArgument::REQUIRED, 'The DQL query to execute', null),
32     ));
33
34     $this->addOptions(array(
35       new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', true),
36       new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
37       new sfCommandOption('show-sql', null, sfCommandOption::PARAMETER_NONE, 'Show the sql that would be executed'),
38     ));
39
40     $this->aliases = array('doctrine-dql');
41     $this->namespace = 'doctrine';
42     $this->name = 'dql';
43     $this->briefDescription = 'Execute a DQL query and view the results';
44
45     $this->detailedDescription = <<<EOF
46 The [doctrine:data-dql|INFO] task executes a DQL query and display the formatted results:
47
48   [./symfony doctrine:dql "FROM User u"|INFO]
49
50 You can show the SQL that would be executed by using the [--dir|COMMENT] option:
51
52   [./symfony doctrine:dql --show-sql "FROM User u"|INFO]
53 EOF;
54   }
55
56   /**
57    * @see sfTask
58    */
59   protected function execute($arguments = array(), $options = array())
60   {
61     $databaseManager = new sfDatabaseManager($this->configuration);
62
63     $dql = $arguments['dql_query'];
64
65     $q = Doctrine_Query::create()
66       ->parseQuery($dql);
67
68     $this->logSection('doctrine', 'executing dql query');
69
70     echo sprintf('DQL: %s', $dql) . "\n";
71
72     if ($options['show-sql']) {
73       echo sprintf('SQL: %s', $q->getSql()) . "\n";
74     }
75
76     $count = $q->count();
77
78     if ($count)
79     {
80       echo sprintf('found %s results', $count) . "\n";
81
82       $results = $q->fetchArray();
83       $yaml = sfYaml::dump($results, 4);
84       $lines = explode("\n", $yaml);
85       foreach ($lines as $line)
86       {
87         echo $line . "\n";
88       }
89     } else {
90       $this->logSection('doctrine', 'no results found');
91     }
92   }
93 }
Note: See TracBrowser for help on using the browser.