2007-10-20 21:20:56 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id: Dql.php 2761 2007-10-07 23:42:29Z zYne $
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
2008-01-23 01:52:53 +03:00
|
|
|
* <http://www.phpdoctrine.org>.
|
2007-10-20 21:20:56 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Doctrine_Task_Dql
|
|
|
|
*
|
|
|
|
* @package Doctrine
|
|
|
|
* @subpackage Task
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
2008-02-22 21:11:35 +03:00
|
|
|
* @link www.phpdoctrine.org
|
2007-10-20 21:20:56 +04:00
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision: 2761 $
|
|
|
|
* @author Jonathan H. Wage <jwage@mac.com>
|
|
|
|
*/
|
|
|
|
class Doctrine_Task_Dql extends Doctrine_Task
|
|
|
|
{
|
|
|
|
public $description = 'Execute dql query and display the results',
|
|
|
|
$requiredArguments = array('models_path' => 'Specify path to your Doctrine_Record definitions.',
|
2008-01-24 02:27:02 +03:00
|
|
|
'dql_query' => 'Specify the complete dql query to execute.'),
|
|
|
|
$optionalArguments = array('params' => 'Comma separated list of the params to replace the ? tokens in the dql');
|
2008-01-25 05:53:24 +03:00
|
|
|
|
2007-10-20 21:20:56 +04:00
|
|
|
public function execute()
|
|
|
|
{
|
|
|
|
Doctrine::loadModels($this->getArgument('models_path'));
|
2008-01-25 05:53:24 +03:00
|
|
|
|
2007-10-20 21:20:56 +04:00
|
|
|
$dql = $this->getArgument('dql_query');
|
2008-01-25 05:53:24 +03:00
|
|
|
|
2007-10-20 21:20:56 +04:00
|
|
|
$query = new Doctrine_Query();
|
2008-01-25 05:53:24 +03:00
|
|
|
|
2008-02-18 01:52:37 +03:00
|
|
|
$params = $this->getArgument('params');
|
2008-03-02 19:51:09 +03:00
|
|
|
$params = $params ? explode(',', $params):array();
|
2008-01-25 05:53:24 +03:00
|
|
|
|
|
|
|
$this->notify('executing: "' . $dql . '" (' . implode(', ', $params) . ')');
|
2008-01-30 02:19:20 +03:00
|
|
|
|
2008-02-18 01:52:37 +03:00
|
|
|
$results = $query->query($dql);
|
2008-01-25 05:53:24 +03:00
|
|
|
|
2008-01-25 06:18:51 +03:00
|
|
|
$this->_printResults($results);
|
2007-10-20 21:20:56 +04:00
|
|
|
}
|
2008-01-25 05:53:24 +03:00
|
|
|
|
2008-01-25 06:18:51 +03:00
|
|
|
protected function _printResults($data)
|
2007-10-20 21:20:56 +04:00
|
|
|
{
|
|
|
|
$array = $data->toArray(true);
|
2008-01-25 05:53:24 +03:00
|
|
|
|
2007-10-20 21:20:56 +04:00
|
|
|
$yaml = Doctrine_Parser::dump($array, 'yml');
|
|
|
|
$lines = explode("\n", $yaml);
|
2008-01-25 05:53:24 +03:00
|
|
|
|
2007-10-20 21:20:56 +04:00
|
|
|
unset($lines[0]);
|
|
|
|
$lines[1] = $data->getTable()->getOption('name') . ':';
|
2008-01-25 05:53:24 +03:00
|
|
|
|
2007-10-20 21:20:56 +04:00
|
|
|
foreach ($lines as $yamlLine) {
|
|
|
|
$line = trim($yamlLine);
|
2008-01-25 05:53:24 +03:00
|
|
|
|
2007-10-20 21:20:56 +04:00
|
|
|
if ($line) {
|
|
|
|
$this->notify($yamlLine);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-01-25 05:53:24 +03:00
|
|
|
}
|