2009-07-09 08:18:58 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Doctrine\ORM;
|
|
|
|
|
|
|
|
use Doctrine\ORM\Query\Expr;
|
|
|
|
|
|
|
|
/**
|
2009-07-10 11:38:42 +04:00
|
|
|
* This class is responsible for building DQL query strings via an object oriented
|
|
|
|
* PHP interface.
|
2009-07-09 08:18:58 +04:00
|
|
|
*
|
2009-08-20 07:21:39 +04:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link www.doctrine-project.org
|
|
|
|
* @since 2.0
|
|
|
|
* @version $Revision$
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @author Jonathan Wage <jonwage@gmail.com>
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2009-07-09 08:18:58 +04:00
|
|
|
*/
|
|
|
|
class QueryBuilder
|
|
|
|
{
|
|
|
|
const SELECT = 0;
|
|
|
|
const DELETE = 1;
|
|
|
|
const UPDATE = 2;
|
|
|
|
|
|
|
|
const STATE_DIRTY = 0;
|
|
|
|
const STATE_CLEAN = 1;
|
|
|
|
|
2009-07-10 01:56:34 +04:00
|
|
|
/**
|
2009-07-30 19:16:02 +04:00
|
|
|
* @var EntityManager $em Instance of an EntityManager to use for query.
|
2009-07-10 01:56:34 +04:00
|
|
|
*/
|
2009-07-10 21:53:48 +04:00
|
|
|
private $_em;
|
2009-07-10 01:56:34 +04:00
|
|
|
|
|
|
|
/**
|
2009-07-30 19:16:02 +04:00
|
|
|
* @var array $dqlParts The array of DQL parts collected.
|
2009-07-10 01:56:34 +04:00
|
|
|
*/
|
2009-07-10 21:53:48 +04:00
|
|
|
private $_dqlParts = array(
|
2009-08-20 06:59:42 +04:00
|
|
|
'select' => array(),
|
|
|
|
'from' => null,
|
|
|
|
'join' => array(),
|
|
|
|
'set' => array(),
|
|
|
|
'where' => null,
|
2009-07-09 08:18:58 +04:00
|
|
|
'groupBy' => array(),
|
2009-08-20 06:59:42 +04:00
|
|
|
'having' => null,
|
2009-07-10 21:53:48 +04:00
|
|
|
'orderBy' => array()
|
2009-07-09 08:18:58 +04:00
|
|
|
);
|
2009-07-10 01:56:34 +04:00
|
|
|
|
|
|
|
/**
|
2009-07-30 19:16:02 +04:00
|
|
|
* @var integer The type of query this is. Can be select, update or delete.
|
2009-07-10 01:56:34 +04:00
|
|
|
*/
|
2009-07-10 21:53:48 +04:00
|
|
|
private $_type = self::SELECT;
|
2009-07-10 01:56:34 +04:00
|
|
|
|
|
|
|
/**
|
2009-07-30 19:16:02 +04:00
|
|
|
* @var integer The state of the query object. Can be dirty or clean.
|
2009-07-10 01:56:34 +04:00
|
|
|
*/
|
2009-07-10 21:53:48 +04:00
|
|
|
private $_state = self::STATE_CLEAN;
|
2009-07-10 01:56:34 +04:00
|
|
|
|
|
|
|
/**
|
2009-07-30 19:16:02 +04:00
|
|
|
* @var string The complete DQL string for this query.
|
2009-07-10 01:56:34 +04:00
|
|
|
*/
|
2009-07-10 21:53:48 +04:00
|
|
|
private $_dql;
|
2009-07-09 08:18:58 +04:00
|
|
|
|
2009-07-10 01:56:34 +04:00
|
|
|
/**
|
2009-07-30 19:16:02 +04:00
|
|
|
* @var Query The Query instance used for this QueryBuilder.
|
2009-07-10 01:56:34 +04:00
|
|
|
*/
|
2009-07-10 22:26:43 +04:00
|
|
|
private $_q;
|
2009-09-09 02:19:03 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Expr The Expr instance used to generate DQL expressions
|
|
|
|
*/
|
|
|
|
private $_expr;
|
|
|
|
|
2009-07-30 19:16:02 +04:00
|
|
|
/**
|
|
|
|
* Initializes a new <tt>QueryBuilder</tt> that uses the given <tt>EntityManager</tt>.
|
|
|
|
*
|
|
|
|
* @param EntityManager $entityManager The EntityManager to use.
|
|
|
|
*/
|
2009-07-09 08:18:58 +04:00
|
|
|
public function __construct(EntityManager $entityManager)
|
|
|
|
{
|
2009-07-10 01:56:34 +04:00
|
|
|
$this->_em = $entityManager;
|
2009-07-10 22:26:43 +04:00
|
|
|
$this->_q = $entityManager->createQuery();
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-09 02:19:03 +04:00
|
|
|
/**
|
|
|
|
* Factory for instantiating and retrieving the Expr instance when needed
|
|
|
|
*
|
2009-09-10 00:34:42 +04:00
|
|
|
* [php]
|
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
* ->where($qb->expr()->eq('u.id', 1));
|
|
|
|
*
|
2009-09-09 02:19:03 +04:00
|
|
|
* @return Expr $expr
|
|
|
|
*/
|
|
|
|
public function expr()
|
|
|
|
{
|
|
|
|
if ( ! $this->_expr) {
|
|
|
|
$this->_expr = new Expr;
|
|
|
|
}
|
|
|
|
return $this->_expr;
|
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
|
|
|
* Get the type of this query instance. Either the constant for SELECT, UPDATE or DELETE
|
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* switch ($qb->getType())
|
|
|
|
* {
|
|
|
|
* case 0:
|
|
|
|
* echo 'SELECT';
|
|
|
|
* break;
|
|
|
|
*
|
|
|
|
* case 1:
|
|
|
|
* echo 'DELETE';
|
|
|
|
* break;
|
|
|
|
*
|
|
|
|
* case 2:
|
|
|
|
* echo 'UPDATE';
|
|
|
|
* break;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* @return integer $type
|
|
|
|
*/
|
2009-07-09 08:18:58 +04:00
|
|
|
public function getType()
|
|
|
|
{
|
|
|
|
return $this->_type;
|
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
|
|
|
* Get the entity manager instance for this query builder instance
|
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* $em = $qb->getEntityManager();
|
|
|
|
*
|
|
|
|
* @return EntityManager $em
|
|
|
|
*/
|
2009-07-10 01:56:34 +04:00
|
|
|
public function getEntityManager()
|
|
|
|
{
|
|
|
|
return $this->_em;
|
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
|
|
|
* Get the state of this query builder instance
|
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* if ($qb->getState() == QueryBuilder::STATE_DIRTY) {
|
|
|
|
* echo 'Query builder is dirty';
|
|
|
|
* } else {
|
|
|
|
* echo 'Query builder is clean';
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* @return integer $state
|
|
|
|
*/
|
2009-07-09 08:18:58 +04:00
|
|
|
public function getState()
|
|
|
|
{
|
|
|
|
return $this->_state;
|
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
|
|
|
* Get the complete DQL string for this query builder instance
|
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
* echo $qb->getDql(); // SELECT u FROM User u
|
|
|
|
*
|
|
|
|
* @return string $dql The DQL string
|
|
|
|
*/
|
2009-07-09 08:18:58 +04:00
|
|
|
public function getDql()
|
|
|
|
{
|
2009-08-14 22:22:41 +04:00
|
|
|
if ($this->_dql !== null && $this->_state === self::STATE_CLEAN) {
|
2009-07-09 08:18:58 +04:00
|
|
|
return $this->_dql;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dql = '';
|
|
|
|
|
|
|
|
switch ($this->_type) {
|
|
|
|
case self::DELETE:
|
|
|
|
$dql = $this->_getDqlForDelete();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case self::UPDATE:
|
|
|
|
$dql = $this->_getDqlForUpdate();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case self::SELECT:
|
|
|
|
default:
|
|
|
|
$dql = $this->_getDqlForSelect();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-08-14 22:22:41 +04:00
|
|
|
$this->_state = self::STATE_CLEAN;
|
2009-07-09 08:18:58 +04:00
|
|
|
$this->_dql = $dql;
|
|
|
|
|
|
|
|
return $dql;
|
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
|
|
|
* Get the Query instance with the DQL string set to it
|
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u');
|
|
|
|
* $q = $qb->getQuery();
|
|
|
|
* $results = $q->execute();
|
|
|
|
*
|
|
|
|
* @return Query $q
|
|
|
|
*/
|
2009-07-09 08:18:58 +04:00
|
|
|
public function getQuery()
|
|
|
|
{
|
2009-07-10 22:26:43 +04:00
|
|
|
$this->_q->setDql($this->getDql());
|
2009-07-09 08:18:58 +04:00
|
|
|
|
2009-07-10 22:26:43 +04:00
|
|
|
return $this->_q;
|
2009-07-10 01:56:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a query parameter.
|
|
|
|
*
|
2009-09-10 00:34:42 +04:00
|
|
|
* [php]
|
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
* ->where('u.id = :user_id')
|
|
|
|
* ->setParameter(':user_id', 1);
|
|
|
|
*
|
2009-07-10 01:56:34 +04:00
|
|
|
* @param string|integer $key The parameter position or name.
|
|
|
|
* @param mixed $value The parameter value.
|
2009-09-10 00:34:42 +04:00
|
|
|
* @return QueryBuilder $qb
|
2009-07-10 01:56:34 +04:00
|
|
|
*/
|
|
|
|
public function setParameter($key, $value)
|
|
|
|
{
|
2009-07-10 22:26:43 +04:00
|
|
|
$this->_q->setParameter($key, $value);
|
2009-07-10 01:56:34 +04:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a collection of query parameters.
|
|
|
|
*
|
2009-09-10 00:34:42 +04:00
|
|
|
* [php]
|
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
* ->where('u.id = :user_id1 OR u.id = :user_id2')
|
|
|
|
* ->setParameters(array(
|
|
|
|
* ':user_id1' => 1,
|
|
|
|
* ':user_id2' => 2
|
|
|
|
* ));
|
|
|
|
*
|
2009-07-10 01:56:34 +04:00
|
|
|
* @param array $params
|
2009-09-10 00:34:42 +04:00
|
|
|
* @return QueryBuilder $qb
|
2009-07-10 01:56:34 +04:00
|
|
|
*/
|
|
|
|
public function setParameters(array $params)
|
|
|
|
{
|
2009-07-10 22:26:43 +04:00
|
|
|
$this->_q->setParameters($params);
|
2009-07-10 01:56:34 +04:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all defined parameters
|
|
|
|
*
|
|
|
|
* @return array Defined parameters
|
|
|
|
*/
|
|
|
|
public function getParameters($params = array())
|
|
|
|
{
|
2009-07-10 22:26:43 +04:00
|
|
|
return $this->_q->getParameters($params);
|
2009-07-10 01:56:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a query parameter.
|
|
|
|
*
|
|
|
|
* @param mixed $key The key (index or name) of the bound parameter.
|
|
|
|
* @return mixed The value of the bound parameter.
|
|
|
|
*/
|
|
|
|
public function getParameter($key)
|
|
|
|
{
|
2009-07-10 22:26:43 +04:00
|
|
|
return $this->_q->getParameter($key);
|
2009-07-10 01:56:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a single DQL query part to the array of parts
|
|
|
|
*
|
|
|
|
* @param string $dqlPartName
|
|
|
|
* @param string $dqlPart
|
|
|
|
* @param string $append
|
2009-09-10 00:34:42 +04:00
|
|
|
* @return QueryBuilder $qb
|
2009-07-10 01:56:34 +04:00
|
|
|
*/
|
|
|
|
public function add($dqlPartName, $dqlPart, $append = false)
|
|
|
|
{
|
2009-08-20 06:59:42 +04:00
|
|
|
$isMultiple = is_array($this->_dqlParts[$dqlPartName]);
|
|
|
|
|
|
|
|
if ($append && $isMultiple) {
|
2009-07-10 01:56:34 +04:00
|
|
|
$this->_dqlParts[$dqlPartName][] = $dqlPart;
|
|
|
|
} else {
|
2009-08-20 06:59:42 +04:00
|
|
|
$this->_dqlParts[$dqlPartName] = ($isMultiple) ? array($dqlPart) : $dqlPart;
|
2009-07-10 01:56:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->_state = self::STATE_DIRTY;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
|
|
|
* Add to the SELECT statement
|
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u', 'p')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
* ->leftJoin('u.Phonenumbers', 'p');
|
|
|
|
*
|
|
|
|
* @param mixed $select String SELECT statement or SELECT Expr instance
|
|
|
|
* @return QueryBuilder $qb
|
|
|
|
*/
|
2009-07-10 21:53:48 +04:00
|
|
|
public function select($select = null)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
|
|
|
$this->_type = self::SELECT;
|
2009-08-20 06:59:42 +04:00
|
|
|
$selects = func_get_args();
|
2009-07-09 08:18:58 +04:00
|
|
|
|
2009-07-10 01:56:34 +04:00
|
|
|
if (empty($selects)) {
|
2009-07-09 08:18:58 +04:00
|
|
|
return $this;
|
|
|
|
}
|
2009-08-20 06:59:42 +04:00
|
|
|
|
|
|
|
return $this->add('select', new Expr\Select($selects), true);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
|
|
|
* Construct a DQL DELETE query
|
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->delete('User', 'u')
|
|
|
|
* ->where('u.id = :user_id');
|
|
|
|
* ->setParameter(':user_id', 1);
|
|
|
|
*
|
|
|
|
* @param string $delete The model to delete
|
|
|
|
* @param string $alias The alias of the model
|
|
|
|
* @return QueryBuilder $qb
|
|
|
|
*/
|
2009-07-09 08:18:58 +04:00
|
|
|
public function delete($delete = null, $alias = null)
|
|
|
|
{
|
|
|
|
$this->_type = self::DELETE;
|
|
|
|
|
|
|
|
if ( ! $delete) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2009-08-20 06:59:42 +04:00
|
|
|
return $this->add('from', new Expr\From($delete, $alias));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
|
|
|
* Construct a DQL UPDATE query
|
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->update('User', 'u')
|
|
|
|
* ->set('u.password', md5('password'))
|
|
|
|
* ->where('u.id = ?');
|
|
|
|
*
|
|
|
|
* @param string $update The model to update
|
|
|
|
* @param string $alias The alias of the model
|
|
|
|
* @return QueryBuilder $qb
|
|
|
|
*/
|
2009-07-09 08:18:58 +04:00
|
|
|
public function update($update = null, $alias = null)
|
|
|
|
{
|
|
|
|
$this->_type = self::UPDATE;
|
|
|
|
|
|
|
|
if ( ! $update) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2009-08-20 06:59:42 +04:00
|
|
|
return $this->add('from', new Expr\From($update, $alias));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
|
|
|
* Specify the FROM part when constructing a SELECT DQL query
|
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
*
|
|
|
|
* @param string $from The model name
|
|
|
|
* @param string $alias The alias of the model
|
|
|
|
* @return QueryBuilder $qb
|
|
|
|
*/
|
2009-08-15 00:46:43 +04:00
|
|
|
public function from($from, $alias = null)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-08-20 06:59:42 +04:00
|
|
|
return $this->add('from', new Expr\From($from, $alias));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
2009-09-10 00:34:42 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a INNER JOIN
|
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
* ->innerJoin('u.Phonenumbers', 'p', Expr\Join::WITH, 'p.is_primary = 1');
|
|
|
|
*
|
|
|
|
* @param string $join The relationship to join
|
|
|
|
* @param string $alias The alias of the join
|
|
|
|
* @param string $conditionType The condition type constant. Either ON or WITH.
|
|
|
|
* @param string $condition The condition for the join
|
|
|
|
* @return QueryBuilder $qb
|
|
|
|
*/
|
2009-08-15 02:50:36 +04:00
|
|
|
public function innerJoin($join, $alias = null, $conditionType = null, $condition = null)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-08-20 06:59:42 +04:00
|
|
|
return $this->add('join', new Expr\Join(
|
|
|
|
Expr\Join::INNER_JOIN, $join, $alias, $conditionType, $condition
|
|
|
|
), true);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
|
|
|
* Add a LEFT JOIN
|
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
* ->leftJoin('u.Phonenumbers', 'p', Expr\Join::WITH, 'p.is_primary = 1');
|
|
|
|
*
|
|
|
|
* @param string $join The relationship to join
|
|
|
|
* @param string $alias The alias of the join
|
|
|
|
* @param string $conditionType The condition type constant. Either ON or WITH.
|
|
|
|
* @param string $condition The condition for the join
|
|
|
|
* @return QueryBuilder $qb
|
|
|
|
*/
|
2009-08-15 02:50:36 +04:00
|
|
|
public function leftJoin($join, $alias = null, $conditionType = null, $condition = null)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-08-20 06:59:42 +04:00
|
|
|
return $this->add('join', new Expr\Join(
|
|
|
|
Expr\Join::LEFT_JOIN, $join, $alias, $conditionType, $condition
|
|
|
|
), true);
|
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
|
|
|
* Add a SET statement for a DQL UPDATE query
|
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->update('User', 'u')
|
|
|
|
* ->set('u.password', md5('password'))
|
|
|
|
* ->where('u.id = ?');
|
|
|
|
*
|
|
|
|
* @param string $key The key/field to set
|
|
|
|
* @param string $value The value, expression, placeholder, etc. to use in the SET
|
|
|
|
* @return QueryBuilder $qb
|
|
|
|
*/
|
2009-08-20 06:59:42 +04:00
|
|
|
public function set($key, $value)
|
|
|
|
{
|
|
|
|
return $this->add('set', new Expr\Comparison($key, Expr\Comparison::EQ, $value), true);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
|
|
|
* Set and override any existing WHERE statements
|
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
* ->where('u.id = ?');
|
|
|
|
*
|
|
|
|
* // You can optionally programatically build and/or expressions
|
|
|
|
* $qb = $em->createQueryBuilder();
|
|
|
|
*
|
|
|
|
* $or = $qb->expr()->orx();
|
|
|
|
* $or->add($qb->expr()->eq('u.id', 1));
|
|
|
|
* $or->add($qb->expr()->eq('u.id', 2));
|
|
|
|
*
|
|
|
|
* $qb->update('User', 'u')
|
|
|
|
* ->set('u.password', md5('password'))
|
|
|
|
* ->where($or);
|
|
|
|
*
|
|
|
|
* @param mixed $where The WHERE statement
|
|
|
|
* @return QueryBuilder $qb
|
|
|
|
*/
|
2009-07-09 08:18:58 +04:00
|
|
|
public function where($where)
|
|
|
|
{
|
2009-08-20 06:59:42 +04:00
|
|
|
if ( ! (func_num_args() == 1 && ($where instanceof Expr\Andx || $where instanceof Expr\Orx))) {
|
|
|
|
$where = new Expr\Andx(func_get_args());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->add('where', $where);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
|
|
|
* Add a new WHERE statement with an AND
|
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
* ->where('u.username LIKE ?')
|
|
|
|
* ->andWhere('u.is_active = 1');
|
|
|
|
*
|
|
|
|
* @param mixed $where The WHERE statement
|
|
|
|
* @return QueryBuilder $qb
|
|
|
|
* @see where()
|
|
|
|
*/
|
2009-07-10 18:02:06 +04:00
|
|
|
public function andWhere($where)
|
|
|
|
{
|
2009-08-20 06:59:42 +04:00
|
|
|
$where = $this->_getDqlQueryPart('where');
|
|
|
|
$args = func_get_args();
|
|
|
|
|
|
|
|
if ($where instanceof Expr\Andx) {
|
|
|
|
$where->addMultiple($args);
|
|
|
|
} else {
|
|
|
|
array_unshift($args, $where);
|
|
|
|
$where = new Expr\Andx($args);
|
2009-07-10 18:02:06 +04:00
|
|
|
}
|
2009-08-20 06:59:42 +04:00
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
return $this->add('where', $where, true);
|
2009-07-10 18:02:06 +04:00
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
|
|
|
* Add a new WHERE statement with an OR
|
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
* ->where('u.id = 1')
|
|
|
|
* ->orWhere('u.id = 2');
|
|
|
|
*
|
|
|
|
* @param mixed $where The WHERE statement
|
|
|
|
* @return QueryBuilder $qb
|
|
|
|
* @see where()
|
|
|
|
*/
|
2009-07-10 18:02:06 +04:00
|
|
|
public function orWhere($where)
|
|
|
|
{
|
2009-08-20 06:59:42 +04:00
|
|
|
$where = $this->_getDqlQueryPart('where');
|
|
|
|
$args = func_get_args();
|
|
|
|
|
|
|
|
if ($where instanceof Expr\Orx) {
|
|
|
|
$where->addMultiple($args);
|
|
|
|
} else {
|
|
|
|
array_unshift($args, $where);
|
|
|
|
$where = new Expr\Orx($args);
|
2009-07-10 18:02:06 +04:00
|
|
|
}
|
2009-08-20 06:59:42 +04:00
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
return $this->add('where', $where, true);
|
2009-07-10 18:02:06 +04:00
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
|
|
|
* Set the GROUP BY clause
|
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
* ->groupBy('u.id');
|
|
|
|
*
|
|
|
|
* @param string $groupBy The GROUP BY clause
|
|
|
|
* @return QueryBuilder $qb
|
|
|
|
*/
|
2009-07-09 08:18:58 +04:00
|
|
|
public function groupBy($groupBy)
|
|
|
|
{
|
2009-08-20 07:21:39 +04:00
|
|
|
return $this->add('groupBy', new Expr\GroupBy(func_get_args()));
|
2009-07-10 21:53:48 +04:00
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add to the existing GROUP BY clause
|
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
* ->groupBy('u.last_login');
|
|
|
|
* ->addGroupBy('u.created_at')
|
|
|
|
*
|
|
|
|
* @param string $groupBy The GROUP BY clause
|
|
|
|
* @return QueryBuilder $qb
|
|
|
|
*/
|
2009-07-10 21:53:48 +04:00
|
|
|
public function addGroupBy($groupBy)
|
|
|
|
{
|
2009-08-20 07:21:39 +04:00
|
|
|
return $this->add('groupBy', new Expr\GroupBy(func_get_args()), true);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
|
|
|
* Set the HAVING clause
|
|
|
|
*
|
|
|
|
* @param mixed $having
|
|
|
|
* @return QueryBuilder $qb
|
|
|
|
*/
|
2009-07-09 08:18:58 +04:00
|
|
|
public function having($having)
|
|
|
|
{
|
2009-08-20 06:59:42 +04:00
|
|
|
if ( ! (func_num_args() == 1 && ($having instanceof Expr\Andx || $having instanceof Expr\Orx))) {
|
|
|
|
$having = new Expr\Andx(func_get_args());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->add('having', $having);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
|
|
|
* Add to the existing HAVING clause with an AND
|
|
|
|
*
|
|
|
|
* @param mixed $having
|
|
|
|
* @return QueryBuilder $qb
|
|
|
|
*/
|
2009-07-09 08:18:58 +04:00
|
|
|
public function andHaving($having)
|
|
|
|
{
|
2009-08-20 06:59:42 +04:00
|
|
|
$having = $this->_getDqlQueryPart('having');
|
|
|
|
$args = func_get_args();
|
|
|
|
|
|
|
|
if ($having instanceof Expr\Andx) {
|
|
|
|
$having->addMultiple($args);
|
|
|
|
} else {
|
|
|
|
array_unshift($args, $having);
|
|
|
|
$having = new Expr\Andx($args);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
2009-08-20 06:59:42 +04:00
|
|
|
|
|
|
|
return $this->add('having', $having);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
|
|
|
* Add to the existing HAVING clause with an OR
|
|
|
|
*
|
|
|
|
* @param mixed $having
|
|
|
|
* @return QueryBuilder $qb
|
|
|
|
*/
|
2009-07-09 08:18:58 +04:00
|
|
|
public function orHaving($having)
|
|
|
|
{
|
2009-08-20 06:59:42 +04:00
|
|
|
$having = $this->_getDqlQueryPart('having');
|
|
|
|
$args = func_get_args();
|
|
|
|
|
|
|
|
if ($having instanceof Expr\Orx) {
|
|
|
|
$having->addMultiple($args);
|
|
|
|
} else {
|
|
|
|
array_unshift($args, $having);
|
|
|
|
$having = new Expr\Orx($args);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
2009-08-20 06:59:42 +04:00
|
|
|
|
|
|
|
return $this->add('having', $having);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
|
|
|
* Set the ORDER BY clause
|
|
|
|
*
|
|
|
|
* @param string $sort What to sort on
|
|
|
|
* @param string $order Optional: The order to sort the results.
|
|
|
|
* @return QueryBuilder $qb
|
|
|
|
*/
|
2009-08-18 03:30:41 +04:00
|
|
|
public function orderBy($sort, $order = null)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-08-20 07:21:39 +04:00
|
|
|
return $this->add('orderBy', new Expr\OrderBy($sort, $order));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
|
|
|
* Add to the existing ORDER BY clause
|
|
|
|
*
|
|
|
|
* @param string $sort What to sort on
|
|
|
|
* @param string $order Optional: The order to sort the results.
|
|
|
|
* @return QueryBuilder $qb
|
|
|
|
*/
|
2009-08-18 03:30:41 +04:00
|
|
|
public function addOrderBy($sort, $order = null)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-08-20 07:21:39 +04:00
|
|
|
return $this->add('orderBy', new Expr\OrderBy($sort, $order), true);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private function _getDqlForDelete()
|
|
|
|
{
|
|
|
|
return 'DELETE'
|
2009-08-20 06:59:42 +04:00
|
|
|
. $this->_getReducedDqlQueryPart('from', array('pre' => ' '))
|
|
|
|
. $this->_getReducedDqlQueryPart('where', array('pre' => ' WHERE '))
|
2009-07-10 21:53:48 +04:00
|
|
|
. $this->_getReducedDqlQueryPart('orderBy', array('pre' => ' ORDER BY ', 'separator' => ', '));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private function _getDqlForUpdate()
|
|
|
|
{
|
|
|
|
return 'UPDATE'
|
2009-08-20 06:59:42 +04:00
|
|
|
. $this->_getReducedDqlQueryPart('from', array('pre' => ' '))
|
2009-07-09 08:18:58 +04:00
|
|
|
. $this->_getReducedDqlQueryPart('set', array('pre' => ' SET ', 'separator' => ', '))
|
2009-08-20 06:59:42 +04:00
|
|
|
. $this->_getReducedDqlQueryPart('where', array('pre' => ' WHERE '))
|
2009-07-10 21:53:48 +04:00
|
|
|
. $this->_getReducedDqlQueryPart('orderBy', array('pre' => ' ORDER BY ', 'separator' => ', '));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private function _getDqlForSelect()
|
|
|
|
{
|
2009-08-20 06:59:42 +04:00
|
|
|
return 'SELECT'
|
2009-07-09 08:18:58 +04:00
|
|
|
. $this->_getReducedDqlQueryPart('select', array('pre' => ' ', 'separator' => ', '))
|
2009-08-20 06:59:42 +04:00
|
|
|
. $this->_getReducedDqlQueryPart('from', array('pre' => ' FROM '))
|
|
|
|
. $this->_getReducedDqlQueryPart('join', array('pre' => ' ', 'separator' => ' '))
|
|
|
|
. $this->_getReducedDqlQueryPart('where', array('pre' => ' WHERE '))
|
2009-07-09 08:18:58 +04:00
|
|
|
. $this->_getReducedDqlQueryPart('groupBy', array('pre' => ' GROUP BY ', 'separator' => ', '))
|
2009-08-20 06:59:42 +04:00
|
|
|
. $this->_getReducedDqlQueryPart('having', array('pre' => ' HAVING '))
|
2009-07-10 21:53:48 +04:00
|
|
|
. $this->_getReducedDqlQueryPart('orderBy', array('pre' => ' ORDER BY ', 'separator' => ', '));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private function _getReducedDqlQueryPart($queryPartName, $options = array())
|
|
|
|
{
|
2009-08-20 06:59:42 +04:00
|
|
|
$queryPart = $this->_getDqlQueryPart($queryPartName);
|
|
|
|
|
|
|
|
if (empty($queryPart)) {
|
2009-07-09 08:18:58 +04:00
|
|
|
return (isset($options['empty']) ? $options['empty'] : '');
|
|
|
|
}
|
2009-08-20 06:59:42 +04:00
|
|
|
|
|
|
|
return (isset($options['pre']) ? $options['pre'] : '')
|
|
|
|
. (is_array($queryPart) ? implode($options['separator'], $queryPart) : $queryPart)
|
|
|
|
. (isset($options['post']) ? $options['post'] : '');
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-07-10 01:56:34 +04:00
|
|
|
private function _getDqlQueryPart($queryPartName)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
|
|
|
return $this->_dqlParts[$queryPartName];
|
|
|
|
}
|
2009-08-15 02:50:36 +04:00
|
|
|
|
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
return $this->getDql();
|
|
|
|
}
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|