| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
require_once(dirname(__FILE__).'/sfDoctrineBaseTask.class.php'); |
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 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 |
} |
|---|