2009-07-09 08:18:58 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* 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
|
2012-05-26 16:37:00 +04:00
|
|
|
* and is licensed under the MIT license. For more information, see
|
2009-07-09 08:18:58 +04:00
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Doctrine\ORM;
|
|
|
|
|
2012-05-28 20:16:42 +04:00
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
|
|
|
|
2010-02-09 20:13:49 +03:00
|
|
|
use Doctrine\ORM\Query\Expr;
|
2009-07-09 08:18:58 +04:00
|
|
|
|
|
|
|
/**
|
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
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @since 2.0
|
|
|
|
* @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
|
|
|
|
{
|
2010-05-07 15:36:25 +04:00
|
|
|
/* The query types. */
|
2009-07-09 08:18:58 +04:00
|
|
|
const SELECT = 0;
|
|
|
|
const DELETE = 1;
|
|
|
|
const UPDATE = 2;
|
|
|
|
|
2010-05-07 15:36:25 +04:00
|
|
|
/** The builder states. */
|
2009-07-09 08:18:58 +04:00
|
|
|
const STATE_DIRTY = 0;
|
|
|
|
const STATE_CLEAN = 1;
|
|
|
|
|
2009-07-10 01:56:34 +04:00
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* @var EntityManager The EntityManager used by this QueryBuilder.
|
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
|
|
|
|
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* @var array 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(
|
2012-01-25 13:19:01 +04:00
|
|
|
'distinct' => false,
|
2009-08-20 06:59:42 +04:00
|
|
|
'select' => array(),
|
2009-10-04 23:35:47 +04:00
|
|
|
'from' => array(),
|
2009-08-20 06:59:42 +04:00
|
|
|
'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;
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2009-07-10 01:56:34 +04:00
|
|
|
/**
|
2012-05-28 20:16:42 +04:00
|
|
|
* @var \Doctrine\Common\Collections\ArrayCollection The query parameters.
|
2010-07-22 01:16:45 +04:00
|
|
|
*/
|
2012-05-28 20:16:42 +04:00
|
|
|
private $parameters = array();
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2009-09-09 02:19:03 +04:00
|
|
|
/**
|
2010-02-09 20:13:49 +03:00
|
|
|
* @var integer The index of the first result to retrieve.
|
2009-09-09 02:19:03 +04:00
|
|
|
*/
|
2010-02-09 20:13:49 +03:00
|
|
|
private $_firstResult = null;
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2010-02-09 20:13:49 +03:00
|
|
|
/**
|
|
|
|
* @var integer The maximum number of results to retrieve.
|
|
|
|
*/
|
|
|
|
private $_maxResults = null;
|
2009-09-09 02:19:03 +04:00
|
|
|
|
2012-04-05 12:30:00 +04:00
|
|
|
/**
|
2012-04-10 12:47:05 +04:00
|
|
|
* @var array Keeps root entity alias names for join entities.
|
2012-04-05 12:30:00 +04:00
|
|
|
*/
|
|
|
|
private $joinRootAliases = array();
|
|
|
|
|
2009-07-30 19:16:02 +04:00
|
|
|
/**
|
|
|
|
* Initializes a new <tt>QueryBuilder</tt> that uses the given <tt>EntityManager</tt>.
|
2011-11-18 17:41:44 +04:00
|
|
|
*
|
2010-02-09 20:13:49 +03:00
|
|
|
* @param EntityManager $em The EntityManager to use.
|
2009-07-30 19:16:02 +04:00
|
|
|
*/
|
2010-02-09 20:13:49 +03:00
|
|
|
public function __construct(EntityManager $em)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2010-02-09 20:13:49 +03:00
|
|
|
$this->_em = $em;
|
2012-05-28 20:16:42 +04:00
|
|
|
$this->parameters = new ArrayCollection();
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-09 02:19:03 +04:00
|
|
|
/**
|
2010-02-09 20:13:49 +03:00
|
|
|
* Gets an ExpressionBuilder used for object-oriented construction of query expressions.
|
2010-05-07 15:36:25 +04:00
|
|
|
* This producer method is intended for convenient inline usage. Example:
|
2009-09-09 02:19:03 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* <code>
|
2009-09-10 00:34:42 +04:00
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
* ->where($qb->expr()->eq('u.id', 1));
|
2010-05-07 15:36:25 +04:00
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* For more complex expression construction, consider storing the expression
|
|
|
|
* builder object in a local variable.
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2011-12-23 20:05:08 +04:00
|
|
|
* @return Query\Expr
|
2009-09-09 02:19:03 +04:00
|
|
|
*/
|
|
|
|
public function expr()
|
|
|
|
{
|
2010-02-09 20:13:49 +03:00
|
|
|
return $this->_em->getExpressionBuilder();
|
2009-09-09 02:19:03 +04:00
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
2010-02-09 20:13:49 +03:00
|
|
|
* Get the type of the currently built query.
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-02-09 20:13:49 +03:00
|
|
|
* @return integer
|
2009-09-10 00:34:42 +04:00
|
|
|
*/
|
2009-07-09 08:18:58 +04:00
|
|
|
public function getType()
|
|
|
|
{
|
|
|
|
return $this->_type;
|
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
2010-02-09 20:13:49 +03:00
|
|
|
* Get the associated EntityManager for this query builder.
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-02-09 20:13:49 +03:00
|
|
|
* @return EntityManager
|
2009-09-10 00:34:42 +04:00
|
|
|
*/
|
2009-07-10 01:56:34 +04:00
|
|
|
public function getEntityManager()
|
|
|
|
{
|
|
|
|
return $this->_em;
|
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Get the state of this query builder instance.
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @return integer Either QueryBuilder::STATE_DIRTY or QueryBuilder::STATE_CLEAN.
|
2009-09-10 00:34:42 +04:00
|
|
|
*/
|
2009-07-09 08:18:58 +04:00
|
|
|
public function getState()
|
|
|
|
{
|
|
|
|
return $this->_state;
|
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Get the complete DQL string formed by the current specifications of this QueryBuilder.
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* <code>
|
2009-09-10 00:34:42 +04:00
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
* echo $qb->getDql(); // SELECT u FROM User u
|
2010-05-07 15:36:25 +04:00
|
|
|
* </code>
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @return string The DQL query string.
|
2009-09-10 00:34:42 +04:00
|
|
|
*/
|
2010-04-13 02:49:19 +04:00
|
|
|
public function getDQL()
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
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:
|
2010-04-13 02:49:19 +04:00
|
|
|
$dql = $this->_getDQLForDelete();
|
2009-07-09 08:18:58 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case self::UPDATE:
|
2010-04-13 02:49:19 +04:00
|
|
|
$dql = $this->_getDQLForUpdate();
|
2009-07-09 08:18:58 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case self::SELECT:
|
|
|
|
default:
|
2010-04-13 02:49:19 +04:00
|
|
|
$dql = $this->_getDQLForSelect();
|
2009-07-09 08:18:58 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-08-14 22:22:41 +04:00
|
|
|
$this->_state = self::STATE_CLEAN;
|
2012-03-15 09:26:06 +04:00
|
|
|
$this->_dql = $dql;
|
2009-07-09 08:18:58 +04:00
|
|
|
|
|
|
|
return $dql;
|
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Constructs a Query instance from the current specifications of the builder.
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* <code>
|
2009-09-10 00:34:42 +04:00
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u');
|
|
|
|
* $q = $qb->getQuery();
|
|
|
|
* $results = $q->execute();
|
2010-05-07 15:36:25 +04:00
|
|
|
* </code>
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-02-09 20:13:49 +03:00
|
|
|
* @return Query
|
2009-09-10 00:34:42 +04:00
|
|
|
*/
|
2009-07-09 08:18:58 +04:00
|
|
|
public function getQuery()
|
|
|
|
{
|
2012-05-28 20:16:42 +04:00
|
|
|
$parameters = clone $this->parameters;
|
|
|
|
|
2010-04-13 02:49:19 +04:00
|
|
|
return $this->_em->createQuery($this->getDQL())
|
2012-05-28 20:16:42 +04:00
|
|
|
->setParameters($parameters)
|
2012-03-15 09:26:06 +04:00
|
|
|
->setFirstResult($this->_firstResult)
|
|
|
|
->setMaxResults($this->_maxResults);
|
2009-07-10 01:56:34 +04:00
|
|
|
}
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2012-04-10 12:47:05 +04:00
|
|
|
/**
|
|
|
|
* Finds the root entity alias of the joined entity.
|
|
|
|
*
|
|
|
|
* @param string $alias The alias of the new join entity
|
|
|
|
* @param string $parentAlias The parent entity alias of the join relationship
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function findRootAlias($alias, $parentAlias)
|
|
|
|
{
|
|
|
|
$rootAlias = null;
|
|
|
|
|
|
|
|
if (in_array($parentAlias, $this->getRootAliases())) {
|
|
|
|
$rootAlias = $parentAlias;
|
|
|
|
} elseif (isset($this->joinRootAliases[$parentAlias])) {
|
|
|
|
$rootAlias = $this->joinRootAliases[$parentAlias];
|
|
|
|
} else {
|
|
|
|
// Should never happen with correct joining order. Might be
|
|
|
|
// thoughtful to throw exception instead.
|
|
|
|
$rootAlias = $this->getRootAlias();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->joinRootAliases[$alias] = $rootAlias;
|
|
|
|
|
|
|
|
return $rootAlias;
|
|
|
|
}
|
|
|
|
|
2011-05-16 00:11:10 +04:00
|
|
|
/**
|
|
|
|
* Gets the FIRST root alias of the query. This is the first entity alias involved
|
|
|
|
* in the construction of the query.
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* $qb = $em->createQueryBuilder()
|
2012-03-15 09:26:06 +04:00
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u');
|
2011-05-16 00:11:10 +04:00
|
|
|
*
|
|
|
|
* echo $qb->getRootAlias(); // u
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @deprecated Please use $qb->getRootAliases() instead.
|
|
|
|
* @return string $rootAlias
|
|
|
|
*/
|
2012-04-10 12:47:05 +04:00
|
|
|
public function getRootAlias()
|
2011-05-16 00:11:10 +04:00
|
|
|
{
|
2012-04-10 12:47:05 +04:00
|
|
|
$aliases = $this->getRootAliases();
|
|
|
|
return $aliases[0];
|
2011-05-16 00:11:10 +04:00
|
|
|
}
|
2009-07-10 01:56:34 +04:00
|
|
|
|
2009-10-04 23:35:47 +04:00
|
|
|
/**
|
2011-06-09 22:42:40 +04:00
|
|
|
* Gets the root aliases of the query. This is the entity aliases involved
|
2010-05-07 15:36:25 +04:00
|
|
|
* in the construction of the query.
|
2009-10-04 23:35:47 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* <code>
|
2009-10-04 23:35:47 +04:00
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u');
|
|
|
|
*
|
2011-05-14 07:40:23 +04:00
|
|
|
* $qb->getRootAliases(); // array('u')
|
2010-05-07 15:36:25 +04:00
|
|
|
* </code>
|
2009-10-04 23:35:47 +04:00
|
|
|
*
|
2011-06-09 22:42:40 +04:00
|
|
|
* @return array $rootAliases
|
2009-10-04 23:35:47 +04:00
|
|
|
*/
|
2011-05-14 07:40:23 +04:00
|
|
|
public function getRootAliases()
|
2009-10-04 23:35:47 +04:00
|
|
|
{
|
2011-05-14 07:40:23 +04:00
|
|
|
$aliases = array();
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2011-05-14 07:40:23 +04:00
|
|
|
foreach ($this->_dqlParts['from'] as &$fromClause) {
|
|
|
|
if (is_string($fromClause)) {
|
|
|
|
$spacePos = strrpos($fromClause, ' ');
|
|
|
|
$from = substr($fromClause, 0, $spacePos);
|
|
|
|
$alias = substr($fromClause, $spacePos + 1);
|
|
|
|
|
|
|
|
$fromClause = new Query\Expr\From($from, $alias);
|
|
|
|
}
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2011-05-14 07:40:23 +04:00
|
|
|
$aliases[] = $fromClause->getAlias();
|
|
|
|
}
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2011-05-14 07:40:23 +04:00
|
|
|
return $aliases;
|
2009-10-04 23:35:47 +04:00
|
|
|
}
|
|
|
|
|
2011-06-09 22:42:40 +04:00
|
|
|
/**
|
|
|
|
* Gets the root entities of the query. This is the entity aliases involved
|
|
|
|
* in the construction of the query.
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u');
|
|
|
|
*
|
|
|
|
* $qb->getRootEntities(); // array('User')
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @return array $rootEntities
|
|
|
|
*/
|
|
|
|
public function getRootEntities()
|
|
|
|
{
|
|
|
|
$entities = array();
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2011-06-09 22:42:40 +04:00
|
|
|
foreach ($this->_dqlParts['from'] as &$fromClause) {
|
|
|
|
if (is_string($fromClause)) {
|
|
|
|
$spacePos = strrpos($fromClause, ' ');
|
|
|
|
$from = substr($fromClause, 0, $spacePos);
|
|
|
|
$alias = substr($fromClause, $spacePos + 1);
|
|
|
|
|
|
|
|
$fromClause = new Query\Expr\From($from, $alias);
|
|
|
|
}
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2011-06-09 22:42:40 +04:00
|
|
|
$entities[] = $fromClause->getFrom();
|
|
|
|
}
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2011-06-09 22:42:40 +04:00
|
|
|
return $entities;
|
|
|
|
}
|
|
|
|
|
2009-07-10 01:56:34 +04:00
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Sets a query parameter for the query being constructed.
|
2009-07-10 01:56:34 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* <code>
|
2009-09-10 00:34:42 +04:00
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
* ->where('u.id = :user_id')
|
2011-11-18 17:41:44 +04:00
|
|
|
* ->setParameter('user_id', 1);
|
2010-05-07 15:36:25 +04:00
|
|
|
* </code>
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2009-07-10 01:56:34 +04:00
|
|
|
* @param string|integer $key The parameter position or name.
|
|
|
|
* @param mixed $value The parameter value.
|
2010-07-22 01:16:45 +04:00
|
|
|
* @param string|null $type PDO::PARAM_* or \Doctrine\DBAL\Types\Type::* constant
|
2010-02-09 20:13:49 +03:00
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
2009-07-10 01:56:34 +04:00
|
|
|
*/
|
2010-07-22 01:16:45 +04:00
|
|
|
public function setParameter($key, $value, $type = null)
|
2009-07-10 01:56:34 +04:00
|
|
|
{
|
2012-05-29 22:52:30 +04:00
|
|
|
$filteredParameters = $this->parameters->filter(
|
|
|
|
function ($parameter) use ($key)
|
|
|
|
{
|
|
|
|
// Must not be identical because of string to integer conversion
|
|
|
|
return ($key == $parameter->getName());
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if (count($filteredParameters)) {
|
|
|
|
$parameter = $filteredParameters->first();
|
|
|
|
$parameter->setValue($value, $type);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parameter = new Query\Parameter($key, $value, $type);
|
|
|
|
|
|
|
|
$this->parameters->add($parameter);
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2009-07-10 01:56:34 +04:00
|
|
|
return $this;
|
|
|
|
}
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2009-07-10 01:56:34 +04:00
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Sets a collection of query parameters for the query being constructed.
|
2009-07-10 01:56:34 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* <code>
|
2009-09-10 00:34:42 +04:00
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
* ->where('u.id = :user_id1 OR u.id = :user_id2')
|
2012-05-28 20:16:42 +04:00
|
|
|
* ->setParameters(new ArrayCollection(array(
|
|
|
|
* new Parameter('user_id1', 1),
|
|
|
|
* new Parameter('user_id2', 2)
|
|
|
|
)));
|
2010-05-07 15:36:25 +04:00
|
|
|
* </code>
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2012-05-29 22:52:30 +04:00
|
|
|
* @param \Doctrine\Common\Collections\ArrayCollection|array $params The query parameters to set.
|
2010-02-09 20:13:49 +03:00
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
2009-07-10 01:56:34 +04:00
|
|
|
*/
|
2012-05-29 22:52:30 +04:00
|
|
|
public function setParameters($parameters)
|
2009-07-10 01:56:34 +04:00
|
|
|
{
|
2012-05-29 22:52:30 +04:00
|
|
|
// BC compatibility with 2.3-
|
|
|
|
if (is_array($parameters)) {
|
|
|
|
$parameterCollection = new ArrayCollection();
|
|
|
|
|
|
|
|
foreach ($parameters as $key => $value) {
|
|
|
|
$parameter = new Query\Parameter($key, $value);
|
|
|
|
|
|
|
|
$parameterCollection->add($parameter);
|
|
|
|
}
|
|
|
|
|
|
|
|
$parameters = $parameterCollection;
|
|
|
|
}
|
|
|
|
|
2012-05-28 20:16:42 +04:00
|
|
|
$this->parameters = $parameters;
|
2011-12-01 19:00:26 +04:00
|
|
|
|
2009-07-10 01:56:34 +04:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Gets all defined query parameters for the query being constructed.
|
2009-07-10 01:56:34 +04:00
|
|
|
*
|
2012-05-29 22:52:30 +04:00
|
|
|
* @return \Doctrine\Common\Collections\ArrayCollection The currently defined query parameters.
|
2009-07-10 01:56:34 +04:00
|
|
|
*/
|
2010-05-07 15:36:25 +04:00
|
|
|
public function getParameters()
|
2009-07-10 01:56:34 +04:00
|
|
|
{
|
2012-05-28 20:16:42 +04:00
|
|
|
return $this->parameters;
|
2009-07-10 01:56:34 +04:00
|
|
|
}
|
2010-05-07 15:36:25 +04:00
|
|
|
|
2009-07-10 01:56:34 +04:00
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Gets a (previously set) query parameter of the query being constructed.
|
2011-11-18 17:41:44 +04:00
|
|
|
*
|
2009-07-10 01:56:34 +04:00
|
|
|
* @param mixed $key The key (index or name) of the bound parameter.
|
2012-05-28 20:16:42 +04:00
|
|
|
*
|
2009-07-10 01:56:34 +04:00
|
|
|
* @return mixed The value of the bound parameter.
|
|
|
|
*/
|
|
|
|
public function getParameter($key)
|
|
|
|
{
|
2012-05-29 22:52:30 +04:00
|
|
|
$filteredParameters = $this->parameters->filter(
|
|
|
|
function ($parameter) use ($key)
|
|
|
|
{
|
|
|
|
// Must not be identical because of string to integer conversion
|
|
|
|
return ($key == $parameter->getName());
|
2012-05-28 20:16:42 +04:00
|
|
|
}
|
2012-05-29 22:52:30 +04:00
|
|
|
);
|
2012-05-28 20:16:42 +04:00
|
|
|
|
2012-05-29 22:52:30 +04:00
|
|
|
return count($filteredParameters) ? $filteredParameters->first() : null;
|
2009-07-10 01:56:34 +04:00
|
|
|
}
|
2010-05-07 15:36:25 +04:00
|
|
|
|
2009-10-02 00:28:53 +04:00
|
|
|
/**
|
|
|
|
* Sets the position of the first result to retrieve (the "offset").
|
|
|
|
*
|
|
|
|
* @param integer $firstResult The first result to return.
|
2010-02-09 20:13:49 +03:00
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
2009-10-02 00:28:53 +04:00
|
|
|
*/
|
|
|
|
public function setFirstResult($firstResult)
|
|
|
|
{
|
2010-02-09 20:13:49 +03:00
|
|
|
$this->_firstResult = $firstResult;
|
2011-12-01 19:00:26 +04:00
|
|
|
|
2009-10-02 00:28:53 +04:00
|
|
|
return $this;
|
|
|
|
}
|
2010-05-07 15:36:25 +04:00
|
|
|
|
2009-10-02 00:28:53 +04:00
|
|
|
/**
|
|
|
|
* Gets the position of the first result the query object was set to retrieve (the "offset").
|
2010-05-07 15:36:25 +04:00
|
|
|
* Returns NULL if {@link setFirstResult} was not applied to this QueryBuilder.
|
2011-11-18 17:41:44 +04:00
|
|
|
*
|
2009-10-02 00:28:53 +04:00
|
|
|
* @return integer The position of the first result.
|
|
|
|
*/
|
|
|
|
public function getFirstResult()
|
|
|
|
{
|
2010-02-09 20:13:49 +03:00
|
|
|
return $this->_firstResult;
|
2009-10-02 00:28:53 +04:00
|
|
|
}
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2009-10-02 00:28:53 +04:00
|
|
|
/**
|
|
|
|
* Sets the maximum number of results to retrieve (the "limit").
|
2011-11-18 17:41:44 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @param integer $maxResults The maximum number of results to retrieve.
|
2010-02-09 20:13:49 +03:00
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
2009-10-02 00:28:53 +04:00
|
|
|
*/
|
|
|
|
public function setMaxResults($maxResults)
|
|
|
|
{
|
2010-02-09 20:13:49 +03:00
|
|
|
$this->_maxResults = $maxResults;
|
2011-12-01 19:00:26 +04:00
|
|
|
|
2009-10-02 00:28:53 +04:00
|
|
|
return $this;
|
|
|
|
}
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2009-10-02 00:28:53 +04:00
|
|
|
/**
|
|
|
|
* Gets the maximum number of results the query object was set to retrieve (the "limit").
|
|
|
|
* Returns NULL if {@link setMaxResults} was not applied to this query builder.
|
2011-11-18 17:41:44 +04:00
|
|
|
*
|
2009-10-02 00:28:53 +04:00
|
|
|
* @return integer Maximum number of results.
|
|
|
|
*/
|
|
|
|
public function getMaxResults()
|
|
|
|
{
|
2010-02-09 20:13:49 +03:00
|
|
|
return $this->_maxResults;
|
2009-10-02 00:28:53 +04:00
|
|
|
}
|
2009-07-10 01:56:34 +04:00
|
|
|
|
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Either appends to or replaces a single, generic query part.
|
|
|
|
*
|
|
|
|
* The available parts are: 'select', 'from', 'join', 'set', 'where',
|
|
|
|
* 'groupBy', 'having' and 'orderBy'.
|
2009-07-10 01:56:34 +04:00
|
|
|
*
|
2011-11-18 17:41:44 +04:00
|
|
|
* @param string $dqlPartName
|
|
|
|
* @param string $dqlPart
|
|
|
|
* @param string $append
|
2010-02-09 20:13:49 +03:00
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
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]);
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2011-05-16 01:16:44 +04:00
|
|
|
// This is introduced for backwards compatibility reasons.
|
|
|
|
// TODO: Remove for 3.0
|
|
|
|
if ($dqlPartName == 'join') {
|
|
|
|
$newDqlPart = array();
|
2012-03-15 09:26:06 +04:00
|
|
|
|
2012-03-24 14:16:32 +04:00
|
|
|
foreach ($dqlPart as $k => $v) {
|
2012-03-15 09:26:06 +04:00
|
|
|
$k = is_numeric($k) ? $this->getRootAlias() : $k;
|
|
|
|
|
|
|
|
$newDqlPart[$k] = $v;
|
2011-05-16 01:16:44 +04:00
|
|
|
}
|
2012-03-15 09:26:06 +04:00
|
|
|
|
2011-05-16 01:16:44 +04:00
|
|
|
$dqlPart = $newDqlPart;
|
|
|
|
}
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2009-08-20 06:59:42 +04:00
|
|
|
if ($append && $isMultiple) {
|
2011-05-12 04:40:27 +04:00
|
|
|
if (is_array($dqlPart)) {
|
|
|
|
$key = key($dqlPart);
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2011-05-12 04:40:27 +04:00
|
|
|
$this->_dqlParts[$dqlPartName][$key][] = $dqlPart[$key];
|
|
|
|
} else {
|
|
|
|
$this->_dqlParts[$dqlPartName][] = $dqlPart;
|
|
|
|
}
|
2009-07-10 01:56:34 +04:00
|
|
|
} 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
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Specifies an item that is to be returned in the query result.
|
|
|
|
* Replaces any previously specified selections, if any.
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* <code>
|
2009-09-10 00:34:42 +04:00
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u', 'p')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
* ->leftJoin('u.Phonenumbers', 'p');
|
2010-05-07 15:36:25 +04:00
|
|
|
* </code>
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @param mixed $select The selection expressions.
|
2010-02-09 20:13:49 +03:00
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
2009-09-10 00:34:42 +04:00
|
|
|
*/
|
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;
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2010-01-14 19:14:41 +03:00
|
|
|
if (empty($select)) {
|
2009-07-09 08:18:58 +04:00
|
|
|
return $this;
|
|
|
|
}
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2010-01-14 19:14:41 +03:00
|
|
|
$selects = is_array($select) ? $select : func_get_args();
|
|
|
|
|
2009-10-04 23:35:47 +04:00
|
|
|
return $this->add('select', new Expr\Select($selects), false);
|
|
|
|
}
|
|
|
|
|
2012-01-25 13:19:01 +04:00
|
|
|
/**
|
|
|
|
* Add a DISTINCT flag to this query.
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->distinct()
|
|
|
|
* ->from('User', 'u');
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @param bool
|
|
|
|
* @return QueryBuilder
|
|
|
|
*/
|
|
|
|
public function distinct($flag = true)
|
|
|
|
{
|
|
|
|
$this->_dqlParts['distinct'] = (bool) $flag;
|
2012-03-15 09:26:06 +04:00
|
|
|
|
2012-01-25 13:19:01 +04:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2009-10-04 23:35:47 +04:00
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Adds an item that is to be returned in the query result.
|
2009-10-04 23:35:47 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* <code>
|
2009-10-04 23:35:47 +04:00
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->addSelect('p')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
* ->leftJoin('u.Phonenumbers', 'p');
|
2010-05-07 15:36:25 +04:00
|
|
|
* </code>
|
2009-10-04 23:35:47 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @param mixed $select The selection expression.
|
2010-02-09 20:13:49 +03:00
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
2009-10-04 23:35:47 +04:00
|
|
|
*/
|
|
|
|
public function addSelect($select = null)
|
|
|
|
{
|
|
|
|
$this->_type = self::SELECT;
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2010-01-14 19:14:41 +03:00
|
|
|
if (empty($select)) {
|
|
|
|
return $this;
|
2009-10-04 23:35:47 +04:00
|
|
|
}
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2010-01-14 19:14:41 +03:00
|
|
|
$selects = is_array($select) ? $select : func_get_args();
|
2009-10-04 23:35:47 +04:00
|
|
|
|
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
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Turns the query being built into a bulk delete query that ranges over
|
|
|
|
* a certain entity type.
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* <code>
|
2009-09-10 00:34:42 +04:00
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->delete('User', 'u')
|
|
|
|
* ->where('u.id = :user_id');
|
2011-11-18 17:41:44 +04:00
|
|
|
* ->setParameter('user_id', 1);
|
2010-05-07 15:36:25 +04:00
|
|
|
* </code>
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @param string $delete The class/type whose instances are subject to the deletion.
|
|
|
|
* @param string $alias The class/type alias used in the constructed query.
|
2010-02-09 20:13:49 +03:00
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
2009-09-10 00:34:42 +04:00
|
|
|
*/
|
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
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Turns the query being built into a bulk update query that ranges over
|
|
|
|
* a certain entity type.
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* <code>
|
2009-09-10 00:34:42 +04:00
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->update('User', 'u')
|
|
|
|
* ->set('u.password', md5('password'))
|
|
|
|
* ->where('u.id = ?');
|
2010-05-07 15:36:25 +04:00
|
|
|
* </code>
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @param string $update The class/type whose instances are subject to the update.
|
|
|
|
* @param string $alias The class/type alias used in the constructed query.
|
2010-02-09 20:13:49 +03:00
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
2009-09-10 00:34:42 +04:00
|
|
|
*/
|
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
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Create and add a query root corresponding to the entity identified by the given alias,
|
|
|
|
* forming a cartesian product with any existing query roots.
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* <code>
|
2009-09-10 00:34:42 +04:00
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
2010-05-07 15:36:25 +04:00
|
|
|
* </code>
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-02-09 20:13:49 +03:00
|
|
|
* @param string $from The class name.
|
|
|
|
* @param string $alias The alias of the class.
|
2011-09-24 01:10:58 +04:00
|
|
|
* @param string $indexBy The index for the from.
|
2010-02-09 20:13:49 +03:00
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
2009-09-10 00:34:42 +04:00
|
|
|
*/
|
2011-09-24 23:10:10 +04:00
|
|
|
public function from($from, $alias, $indexBy = null)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2011-09-24 23:10:10 +04:00
|
|
|
return $this->add('from', new Expr\From($from, $alias, $indexBy), true);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
2010-05-07 15:36:25 +04:00
|
|
|
|
2010-02-09 20:13:49 +03:00
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Creates and adds a join over an entity association to the query.
|
2010-02-09 20:13:49 +03:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* The entities in the joined association will be fetched as part of the query
|
|
|
|
* result if the alias used for the joined association is placed in the select
|
|
|
|
* expressions.
|
|
|
|
*
|
|
|
|
* <code>
|
2010-02-09 20:13:49 +03:00
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
2010-05-07 15:36:25 +04:00
|
|
|
* ->join('u.Phonenumbers', 'p', Expr\Join::WITH, 'p.is_primary = 1');
|
|
|
|
* </code>
|
2010-02-09 20:13:49 +03:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @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
|
2011-05-12 04:40:27 +04:00
|
|
|
* @param string $indexBy The index for the join
|
2010-02-09 20:13:49 +03:00
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
|
|
|
*/
|
2011-05-12 04:40:27 +04:00
|
|
|
public function join($join, $alias, $conditionType = null, $condition = null, $indexBy = null)
|
2010-02-09 20:13:49 +03:00
|
|
|
{
|
2011-05-12 04:40:27 +04:00
|
|
|
return $this->innerJoin($join, $alias, $conditionType, $condition, $indexBy);
|
2010-02-09 20:13:49 +03:00
|
|
|
}
|
2009-09-10 00:34:42 +04:00
|
|
|
|
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Creates and adds a join over an entity association to the query.
|
2011-11-18 17:41:44 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* The entities in the joined association will be fetched as part of the query
|
|
|
|
* result if the alias used for the joined association is placed in the select
|
|
|
|
* expressions.
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
* ->innerJoin('u.Phonenumbers', 'p', Expr\Join::WITH, 'p.is_primary = 1');
|
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @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
|
2011-05-12 04:40:27 +04:00
|
|
|
* @param string $indexBy The index for the join
|
2010-02-09 20:13:49 +03:00
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
2009-09-10 00:34:42 +04:00
|
|
|
*/
|
2011-05-12 04:40:27 +04:00
|
|
|
public function innerJoin($join, $alias, $conditionType = null, $condition = null, $indexBy = null)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2012-04-10 12:47:05 +04:00
|
|
|
$parentAlias = substr($join, 0, strpos($join, '.'));
|
2011-12-01 19:00:26 +04:00
|
|
|
|
2012-04-10 12:47:05 +04:00
|
|
|
$rootAlias = $this->findRootAlias($alias, $parentAlias);
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2011-12-01 19:00:26 +04:00
|
|
|
$join = new Expr\Join(
|
|
|
|
Expr\Join::INNER_JOIN, $join, $alias, $conditionType, $condition, $indexBy
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->add('join', array($rootAlias => $join), true);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Creates and adds a left join over an entity association to the query.
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* The entities in the joined association will be fetched as part of the query
|
|
|
|
* result if the alias used for the joined association is placed in the select
|
|
|
|
* expressions.
|
|
|
|
*
|
|
|
|
* <code>
|
2009-09-10 00:34:42 +04:00
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
* ->leftJoin('u.Phonenumbers', 'p', Expr\Join::WITH, 'p.is_primary = 1');
|
2010-05-07 15:36:25 +04:00
|
|
|
* </code>
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @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
|
2011-05-12 04:40:27 +04:00
|
|
|
* @param string $indexBy The index for the join
|
2010-05-07 15:36:25 +04:00
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
2009-09-10 00:34:42 +04:00
|
|
|
*/
|
2011-05-12 04:40:27 +04:00
|
|
|
public function leftJoin($join, $alias, $conditionType = null, $condition = null, $indexBy = null)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2012-04-10 12:47:05 +04:00
|
|
|
$parentAlias = substr($join, 0, strpos($join, '.'));
|
2011-12-01 19:00:26 +04:00
|
|
|
|
2012-04-10 12:47:05 +04:00
|
|
|
$rootAlias = $this->findRootAlias($alias, $parentAlias);
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2011-12-01 19:00:26 +04:00
|
|
|
$join = new Expr\Join(
|
|
|
|
Expr\Join::LEFT_JOIN, $join, $alias, $conditionType, $condition, $indexBy
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->add('join', array($rootAlias => $join), true);
|
2009-08-20 06:59:42 +04:00
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Sets a new value for a field in a bulk update query.
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* <code>
|
2009-09-10 00:34:42 +04:00
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->update('User', 'u')
|
|
|
|
* ->set('u.password', md5('password'))
|
|
|
|
* ->where('u.id = ?');
|
2010-05-07 15:36:25 +04:00
|
|
|
* </code>
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @param string $key The key/field to set.
|
|
|
|
* @param string $value The value, expression, placeholder, etc.
|
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
2009-09-10 00:34:42 +04:00
|
|
|
*/
|
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
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Specifies one or more restrictions to the query result.
|
|
|
|
* Replaces any previously specified restrictions, if any.
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* <code>
|
2009-09-10 00:34:42 +04:00
|
|
|
* $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);
|
2010-05-07 15:36:25 +04:00
|
|
|
* </code>
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @param mixed $predicates The restriction predicates.
|
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
2009-09-10 00:34:42 +04:00
|
|
|
*/
|
2010-01-30 00:24:29 +03:00
|
|
|
public function where($predicates)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2011-05-08 03:14:04 +04:00
|
|
|
if ( ! (func_num_args() == 1 && $predicates instanceof Expr\Composite)) {
|
2010-01-30 00:24:29 +03:00
|
|
|
$predicates = new Expr\Andx(func_get_args());
|
2009-08-20 06:59:42 +04:00
|
|
|
}
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2010-01-30 00:24:29 +03:00
|
|
|
return $this->add('where', $predicates);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Adds one or more restrictions to the query results, forming a logical
|
|
|
|
* conjunction with any previously specified restrictions.
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* <code>
|
2009-09-10 00:34:42 +04:00
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
* ->where('u.username LIKE ?')
|
|
|
|
* ->andWhere('u.is_active = 1');
|
2010-05-07 15:36:25 +04:00
|
|
|
* </code>
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @param mixed $where The query restrictions.
|
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
2009-09-10 00:34:42 +04:00
|
|
|
* @see where()
|
|
|
|
*/
|
2009-07-10 18:02:06 +04:00
|
|
|
public function andWhere($where)
|
|
|
|
{
|
2010-04-13 02:49:19 +04:00
|
|
|
$where = $this->getDQLPart('where');
|
2012-03-15 09:26:06 +04:00
|
|
|
$args = func_get_args();
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2009-08-20 06:59:42 +04:00
|
|
|
if ($where instanceof Expr\Andx) {
|
|
|
|
$where->addMultiple($args);
|
2011-11-18 17:41:44 +04:00
|
|
|
} else {
|
2009-08-20 06:59:42 +04:00
|
|
|
array_unshift($args, $where);
|
|
|
|
$where = new Expr\Andx($args);
|
2009-07-10 18:02:06 +04:00
|
|
|
}
|
2011-11-18 17:41:44 +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
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Adds one or more restrictions to the query results, forming a logical
|
|
|
|
* disjunction with any previously specified restrictions.
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* <code>
|
2009-09-10 00:34:42 +04:00
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
* ->where('u.id = 1')
|
|
|
|
* ->orWhere('u.id = 2');
|
2010-05-07 15:36:25 +04:00
|
|
|
* </code>
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
|
|
|
* @param mixed $where The WHERE statement
|
|
|
|
* @return QueryBuilder $qb
|
|
|
|
* @see where()
|
|
|
|
*/
|
2009-07-10 18:02:06 +04:00
|
|
|
public function orWhere($where)
|
|
|
|
{
|
2009-10-04 23:35:47 +04:00
|
|
|
$where = $this->getDqlPart('where');
|
2012-03-15 09:26:06 +04:00
|
|
|
$args = func_get_args();
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2009-08-20 06:59:42 +04:00
|
|
|
if ($where instanceof Expr\Orx) {
|
|
|
|
$where->addMultiple($args);
|
2011-11-18 17:41:44 +04:00
|
|
|
} else {
|
2009-08-20 06:59:42 +04:00
|
|
|
array_unshift($args, $where);
|
|
|
|
$where = new Expr\Orx($args);
|
2009-07-10 18:02:06 +04:00
|
|
|
}
|
2011-11-18 17:41:44 +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
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Specifies a grouping over the results of the query.
|
|
|
|
* Replaces any previously specified groupings, if any.
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* <code>
|
2009-09-10 00:34:42 +04:00
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
* ->groupBy('u.id');
|
2010-05-07 15:36:25 +04:00
|
|
|
* </code>
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @param string $groupBy The grouping expression.
|
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
2009-09-10 00:34:42 +04:00
|
|
|
*/
|
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
|
|
|
|
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Adds a grouping expression to the query.
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* <code>
|
2009-09-10 00:34:42 +04:00
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
2010-05-07 15:36:25 +04:00
|
|
|
* ->groupBy('u.lastLogin');
|
|
|
|
* ->addGroupBy('u.createdAt')
|
|
|
|
* </code>
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @param string $groupBy The grouping expression.
|
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
2009-09-10 00:34:42 +04:00
|
|
|
*/
|
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
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Specifies a restriction over the groups of the query.
|
|
|
|
* Replaces any previous having restrictions, if any.
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @param mixed $having The restriction over the groups.
|
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
2009-09-10 00:34:42 +04:00
|
|
|
*/
|
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());
|
|
|
|
}
|
2011-11-18 17:41:44 +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
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Adds a restriction over the groups of the query, forming a logical
|
|
|
|
* conjunction with any existing having restrictions.
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @param mixed $having The restriction to append.
|
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
2009-09-10 00:34:42 +04:00
|
|
|
*/
|
2009-07-09 08:18:58 +04:00
|
|
|
public function andHaving($having)
|
|
|
|
{
|
2009-10-04 23:35:47 +04:00
|
|
|
$having = $this->getDqlPart('having');
|
2012-03-15 09:26:06 +04:00
|
|
|
$args = func_get_args();
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2009-08-20 06:59:42 +04:00
|
|
|
if ($having instanceof Expr\Andx) {
|
|
|
|
$having->addMultiple($args);
|
2011-11-18 17:41:44 +04:00
|
|
|
} else {
|
2009-08-20 06:59:42 +04:00
|
|
|
array_unshift($args, $having);
|
|
|
|
$having = new Expr\Andx($args);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
2011-11-18 17:41:44 +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
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Adds a restriction over the groups of the query, forming a logical
|
|
|
|
* disjunction with any existing having restrictions.
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @param mixed $having The restriction to add.
|
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
2009-09-10 00:34:42 +04:00
|
|
|
*/
|
2009-07-09 08:18:58 +04:00
|
|
|
public function orHaving($having)
|
|
|
|
{
|
2009-10-04 23:35:47 +04:00
|
|
|
$having = $this->getDqlPart('having');
|
2012-03-15 09:26:06 +04:00
|
|
|
$args = func_get_args();
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2009-08-20 06:59:42 +04:00
|
|
|
if ($having instanceof Expr\Orx) {
|
|
|
|
$having->addMultiple($args);
|
2011-11-18 17:41:44 +04:00
|
|
|
} else {
|
2009-08-20 06:59:42 +04:00
|
|
|
array_unshift($args, $having);
|
|
|
|
$having = new Expr\Orx($args);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
2010-04-13 02:49:19 +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
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Specifies an ordering for the query results.
|
|
|
|
* Replaces any previously specified orderings, if any.
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @param string $sort The ordering expression.
|
|
|
|
* @param string $order The ordering direction.
|
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
2009-09-10 00:34:42 +04:00
|
|
|
*/
|
2009-08-18 03:30:41 +04:00
|
|
|
public function orderBy($sort, $order = null)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2012-03-15 09:26:06 +04:00
|
|
|
$orderBy = ($sort instanceof Expr\OrderBy) ? $sort : new Expr\OrderBy($sort, $order);
|
|
|
|
|
|
|
|
return $this->add('orderBy', $orderBy);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Adds an ordering to the query results.
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @param string $sort The ordering expression.
|
|
|
|
* @param string $order The ordering direction.
|
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
2009-09-10 00:34:42 +04:00
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2009-10-04 23:35:47 +04:00
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Get a query part by its name.
|
2009-10-04 23:35:47 +04:00
|
|
|
*
|
|
|
|
* @param string $queryPartName
|
|
|
|
* @return mixed $queryPart
|
2010-05-07 15:36:25 +04:00
|
|
|
* @todo Rename: getQueryPart (or remove?)
|
2009-10-04 23:35:47 +04:00
|
|
|
*/
|
2010-04-13 02:49:19 +04:00
|
|
|
public function getDQLPart($queryPartName)
|
2009-10-04 23:35:47 +04:00
|
|
|
{
|
|
|
|
return $this->_dqlParts[$queryPartName];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-05-07 15:36:25 +04:00
|
|
|
* Get all query parts.
|
2009-10-04 23:35:47 +04:00
|
|
|
*
|
|
|
|
* @return array $dqlParts
|
2010-05-07 15:36:25 +04:00
|
|
|
* @todo Rename: getQueryParts (or remove?)
|
2009-10-04 23:35:47 +04:00
|
|
|
*/
|
2010-04-13 02:49:19 +04:00
|
|
|
public function getDQLParts()
|
2009-10-04 23:35:47 +04:00
|
|
|
{
|
|
|
|
return $this->_dqlParts;
|
|
|
|
}
|
|
|
|
|
2010-04-13 02:49:19 +04:00
|
|
|
private function _getDQLForDelete()
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
|
|
|
return 'DELETE'
|
2010-04-13 02:49:19 +04:00
|
|
|
. $this->_getReducedDQLQueryPart('from', array('pre' => ' ', 'separator' => ', '))
|
|
|
|
. $this->_getReducedDQLQueryPart('where', array('pre' => ' WHERE '))
|
|
|
|
. $this->_getReducedDQLQueryPart('orderBy', array('pre' => ' ORDER BY ', 'separator' => ', '));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2010-04-13 02:49:19 +04:00
|
|
|
private function _getDQLForUpdate()
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
|
|
|
return 'UPDATE'
|
2010-04-13 02:49:19 +04:00
|
|
|
. $this->_getReducedDQLQueryPart('from', array('pre' => ' ', 'separator' => ', '))
|
|
|
|
. $this->_getReducedDQLQueryPart('set', array('pre' => ' SET ', 'separator' => ', '))
|
|
|
|
. $this->_getReducedDQLQueryPart('where', array('pre' => ' WHERE '))
|
|
|
|
. $this->_getReducedDQLQueryPart('orderBy', array('pre' => ' ORDER BY ', 'separator' => ', '));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2010-04-13 02:49:19 +04:00
|
|
|
private function _getDQLForSelect()
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2012-01-25 13:19:01 +04:00
|
|
|
$dql = 'SELECT'
|
2012-03-15 09:26:06 +04:00
|
|
|
. ($this->_dqlParts['distinct']===true ? ' DISTINCT' : '')
|
|
|
|
. $this->_getReducedDQLQueryPart('select', array('pre' => ' ', 'separator' => ', '));
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2011-05-12 04:40:27 +04:00
|
|
|
$fromParts = $this->getDQLPart('from');
|
|
|
|
$joinParts = $this->getDQLPart('join');
|
|
|
|
$fromClauses = array();
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2011-05-12 04:40:27 +04:00
|
|
|
// Loop through all FROM clauses
|
|
|
|
if ( ! empty($fromParts)) {
|
|
|
|
$dql .= ' FROM ';
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2011-05-12 04:40:27 +04:00
|
|
|
foreach ($fromParts as $from) {
|
|
|
|
$fromClause = (string) $from;
|
|
|
|
|
2011-06-25 12:25:22 +04:00
|
|
|
if ($from instanceof Expr\From && isset($joinParts[$from->getAlias()])) {
|
2011-05-12 04:40:27 +04:00
|
|
|
foreach ($joinParts[$from->getAlias()] as $join) {
|
|
|
|
$fromClause .= ' ' . ((string) $join);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$fromClauses[] = $fromClause;
|
|
|
|
}
|
|
|
|
}
|
2011-11-18 17:41:44 +04:00
|
|
|
|
|
|
|
$dql .= implode(', ', $fromClauses)
|
2010-04-13 02:49:19 +04:00
|
|
|
. $this->_getReducedDQLQueryPart('where', array('pre' => ' WHERE '))
|
|
|
|
. $this->_getReducedDQLQueryPart('groupBy', array('pre' => ' GROUP BY ', 'separator' => ', '))
|
|
|
|
. $this->_getReducedDQLQueryPart('having', array('pre' => ' HAVING '))
|
|
|
|
. $this->_getReducedDQLQueryPart('orderBy', array('pre' => ' ORDER BY ', 'separator' => ', '));
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2011-05-12 04:40:27 +04:00
|
|
|
return $dql;
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2010-04-13 02:49:19 +04:00
|
|
|
private function _getReducedDQLQueryPart($queryPartName, $options = array())
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2010-04-13 02:49:19 +04:00
|
|
|
$queryPart = $this->getDQLPart($queryPartName);
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2009-08-20 06:59:42 +04:00
|
|
|
if (empty($queryPart)) {
|
2009-07-09 08:18:58 +04:00
|
|
|
return (isset($options['empty']) ? $options['empty'] : '');
|
|
|
|
}
|
2011-11-18 17:41:44 +04:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-09-24 00:32:23 +04:00
|
|
|
/**
|
|
|
|
* Reset DQL parts
|
|
|
|
*
|
|
|
|
* @param array $parts
|
|
|
|
* @return QueryBuilder
|
|
|
|
*/
|
|
|
|
public function resetDQLParts($parts = null)
|
|
|
|
{
|
|
|
|
if (is_null($parts)) {
|
|
|
|
$parts = array_keys($this->_dqlParts);
|
|
|
|
}
|
2012-03-15 09:26:06 +04:00
|
|
|
|
2010-09-24 00:32:23 +04:00
|
|
|
foreach ($parts as $part) {
|
|
|
|
$this->resetDQLPart($part);
|
|
|
|
}
|
2012-03-15 09:26:06 +04:00
|
|
|
|
2010-09-24 00:32:23 +04:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset single DQL part
|
|
|
|
*
|
|
|
|
* @param string $part
|
|
|
|
* @return QueryBuilder;
|
|
|
|
*/
|
|
|
|
public function resetDQLPart($part)
|
|
|
|
{
|
2012-03-15 09:26:06 +04:00
|
|
|
$this->_dqlParts[$part] = is_array($this->_dqlParts[$part]) ? array() : null;
|
|
|
|
$this->_state = self::STATE_DIRTY;
|
|
|
|
|
2010-09-24 00:32:23 +04:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:36:25 +04:00
|
|
|
/**
|
|
|
|
* Gets a string representation of this QueryBuilder which corresponds to
|
|
|
|
* the final DQL query being constructed.
|
|
|
|
*
|
|
|
|
* @return string The string representation of this QueryBuilder.
|
|
|
|
*/
|
2009-08-15 02:50:36 +04:00
|
|
|
public function __toString()
|
|
|
|
{
|
2010-04-13 02:49:19 +04:00
|
|
|
return $this->getDQL();
|
2009-08-15 02:50:36 +04:00
|
|
|
}
|
2010-11-15 23:32:38 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deep clone of all expression objects in the DQL parts.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __clone()
|
|
|
|
{
|
2012-03-24 14:16:32 +04:00
|
|
|
foreach ($this->_dqlParts as $part => $elements) {
|
2010-11-15 23:32:38 +03:00
|
|
|
if (is_array($this->_dqlParts[$part])) {
|
2012-03-24 14:16:32 +04:00
|
|
|
foreach ($this->_dqlParts[$part] as $idx => $element) {
|
2010-11-15 23:32:38 +03:00
|
|
|
if (is_object($element)) {
|
|
|
|
$this->_dqlParts[$part][$idx] = clone $element;
|
|
|
|
}
|
|
|
|
}
|
2012-03-15 09:26:06 +04:00
|
|
|
} else if (is_object($elements)) {
|
2010-11-15 23:32:38 +03:00
|
|
|
$this->_dqlParts[$part] = clone $elements;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-25 13:19:01 +04:00
|
|
|
}
|