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;
|
2012-08-19 21:58:04 +04:00
|
|
|
use Doctrine\Common\Collections\Criteria;
|
2012-05-28 20:16:42 +04:00
|
|
|
|
2010-02-09 20:13:49 +03:00
|
|
|
use Doctrine\ORM\Query\Expr;
|
2012-08-19 21:58:04 +04:00
|
|
|
use Doctrine\ORM\Query\QueryExpressionVisitor;
|
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;
|
|
|
|
|
2012-12-13 21:56: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
|
|
|
/**
|
2012-12-13 21:56:25 +04:00
|
|
|
* The EntityManager used by this QueryBuilder.
|
|
|
|
*
|
2014-12-05 16:02:47 +03:00
|
|
|
* @var EntityManagerInterface
|
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
|
|
|
|
|
|
|
/**
|
2012-12-13 21:56:25 +04:00
|
|
|
* The array of DQL parts collected.
|
|
|
|
*
|
|
|
|
* @var array
|
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
|
|
|
|
|
|
|
/**
|
2012-12-13 21:56:25 +04:00
|
|
|
* The type of query this is. Can be select, update or delete.
|
|
|
|
*
|
|
|
|
* @var integer
|
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
|
|
|
|
|
|
|
/**
|
2012-12-13 21:56:25 +04:00
|
|
|
* The state of the query object. Can be dirty or clean.
|
|
|
|
*
|
|
|
|
* @var integer
|
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
|
|
|
|
|
|
|
/**
|
2012-12-13 21:56:25 +04:00
|
|
|
* The complete DQL string for this query.
|
|
|
|
*
|
|
|
|
* @var string
|
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-12-13 21:56:25 +04:00
|
|
|
* The query parameters.
|
|
|
|
*
|
|
|
|
* @var \Doctrine\Common\Collections\ArrayCollection
|
2010-07-22 01:16:45 +04:00
|
|
|
*/
|
2014-04-05 00:05:16 +04:00
|
|
|
private $parameters;
|
2011-11-18 17:41:44 +04:00
|
|
|
|
2009-09-09 02:19:03 +04:00
|
|
|
/**
|
2012-12-13 21:56:25 +04:00
|
|
|
* The index of the first result to retrieve.
|
|
|
|
*
|
|
|
|
* @var integer
|
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
|
|
|
/**
|
2012-12-13 21:56:25 +04:00
|
|
|
* The maximum number of results to retrieve.
|
|
|
|
*
|
|
|
|
* @var integer
|
2010-02-09 20:13:49 +03:00
|
|
|
*/
|
|
|
|
private $_maxResults = null;
|
2009-09-09 02:19:03 +04:00
|
|
|
|
2012-04-05 12:30:00 +04:00
|
|
|
/**
|
2012-12-13 21:56:25 +04:00
|
|
|
* Keeps root entity alias names for join entities.
|
|
|
|
*
|
|
|
|
* @var array
|
2012-04-05 12:30:00 +04:00
|
|
|
*/
|
|
|
|
private $joinRootAliases = array();
|
|
|
|
|
2014-03-04 00:28:35 +04:00
|
|
|
/**
|
|
|
|
* Whether to use second level cache, if available.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $cacheable = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Second level cache region name.
|
|
|
|
*
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $cacheRegion;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Second level query cache mode.
|
|
|
|
*
|
|
|
|
* @var integer|null
|
|
|
|
*/
|
|
|
|
protected $cacheMode;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
protected $lifetime = 0;
|
|
|
|
|
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
|
|
|
*
|
2014-12-05 16:02:47 +03:00
|
|
|
* @param EntityManagerInterface $em The EntityManager to use.
|
2009-07-30 19:16:02 +04:00
|
|
|
*/
|
2014-12-05 16:02:47 +03:00
|
|
|
public function __construct(EntityManagerInterface $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>
|
2013-01-30 23:11:09 +04:00
|
|
|
* $qb = $em->createQueryBuilder();
|
|
|
|
* $qb
|
2009-09-10 00:34:42 +04:00
|
|
|
* ->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
|
|
|
}
|
|
|
|
|
2014-03-04 00:28:35 +04:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Enable/disable second level query (result) caching for this query.
|
|
|
|
*
|
|
|
|
* @param boolean $cacheable
|
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\AbstractQuery This query instance.
|
|
|
|
*/
|
|
|
|
public function setCacheable($cacheable)
|
|
|
|
{
|
|
|
|
$this->cacheable = (boolean) $cacheable;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean TRUE if the query results are enable for second level cache, FALSE otherwise.
|
|
|
|
*/
|
|
|
|
public function isCacheable()
|
|
|
|
{
|
|
|
|
return $this->cacheable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $cacheRegion
|
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\AbstractQuery This query instance.
|
|
|
|
*/
|
|
|
|
public function setCacheRegion($cacheRegion)
|
|
|
|
{
|
|
|
|
$this->cacheRegion = (string) $cacheRegion;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain the name of the second level query cache region in which query results will be stored
|
|
|
|
*
|
|
|
|
* @return The cache region name; NULL indicates the default region.
|
|
|
|
*/
|
|
|
|
public function getCacheRegion()
|
|
|
|
{
|
|
|
|
return $this->cacheRegion;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function getLifetime()
|
|
|
|
{
|
|
|
|
return $this->lifetime;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the life-time for this query into second level cache.
|
|
|
|
*
|
|
|
|
* @param integer $lifetime
|
|
|
|
* @return \Doctrine\ORM\AbstractQuery This query instance.
|
|
|
|
*/
|
|
|
|
public function setLifetime($lifetime)
|
|
|
|
{
|
|
|
|
$this->lifetime = (integer) $lifetime;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function getCacheMode()
|
|
|
|
{
|
|
|
|
return $this->cacheMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param integer $cacheMode
|
|
|
|
* @return \Doctrine\ORM\AbstractQuery This query instance.
|
|
|
|
*/
|
|
|
|
public function setCacheMode($cacheMode)
|
|
|
|
{
|
|
|
|
$this->cacheMode = (integer) $cacheMode;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2009-09-10 00:34:42 +04:00
|
|
|
/**
|
2012-12-13 21:56:25 +04:00
|
|
|
* Gets 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
|
|
|
/**
|
2012-12-13 21:56:25 +04:00
|
|
|
* Gets 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
|
|
|
/**
|
2012-12-13 21:56:25 +04:00
|
|
|
* Gets 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
|
|
|
/**
|
2012-12-13 21:56:25 +04:00
|
|
|
* Gets 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')
|
2014-01-03 20:28:42 +04:00
|
|
|
* ->from('User', 'u');
|
2009-09-10 00:34:42 +04:00
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2014-03-04 00:28:35 +04:00
|
|
|
$query = $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);
|
2014-03-04 00:28:35 +04:00
|
|
|
|
|
|
|
if ($this->lifetime) {
|
|
|
|
$query->setLifetime($this->lifetime);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->cacheMode) {
|
|
|
|
$query->setCacheMode($this->cacheMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->cacheable) {
|
|
|
|
$query->setCacheable($this->cacheable);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->cacheRegion) {
|
|
|
|
$query->setCacheRegion($this->cacheRegion);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
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.
|
|
|
|
*
|
2012-12-01 20:28:06 +04:00
|
|
|
* @param string $alias The alias of the new join entity
|
2012-04-10 12:47:05 +04:00
|
|
|
* @param string $parentAlias The parent entity alias of the join relationship
|
2012-12-01 20:28:06 +04:00
|
|
|
*
|
2012-04-10 12:47:05 +04:00
|
|
|
* @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.
|
2014-01-04 00:28:06 +04:00
|
|
|
* @throws RuntimeException
|
2012-12-01 20:28:06 +04:00
|
|
|
*
|
|
|
|
* @return string
|
2011-05-16 00:11:10 +04:00
|
|
|
*/
|
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();
|
2014-01-04 00:28:06 +04:00
|
|
|
|
|
|
|
if ( ! isset($aliases[0])) {
|
|
|
|
throw new \RuntimeException('No alias was set before invoking getRootAlias().');
|
|
|
|
}
|
|
|
|
|
2012-04-10 12:47:05 +04:00
|
|
|
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
|
|
|
*
|
2012-12-01 20:28:06 +04:00
|
|
|
* @return array
|
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
|
|
|
}
|
|
|
|
|
2014-12-06 17:40:03 +03:00
|
|
|
/**
|
|
|
|
* Gets all the aliases that have been used in the query.
|
|
|
|
* Including all select root aliases and join aliases
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u')
|
|
|
|
* ->join('u.articles','a';
|
|
|
|
*
|
|
|
|
* $qb->getAllAliases(); // array('u','a')
|
|
|
|
* </code>
|
|
|
|
* @return array
|
|
|
|
*/
|
2015-02-14 23:35:02 +03:00
|
|
|
public function getAllAliases() {
|
2014-12-06 17:40:03 +03:00
|
|
|
return array_merge($this->getRootAliases(),array_keys($this->joinRootAliases));
|
|
|
|
}
|
|
|
|
|
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>
|
|
|
|
*
|
2012-12-01 20:28:06 +04:00
|
|
|
* @return array
|
2011-06-09 22:42:40 +04:00
|
|
|
*/
|
|
|
|
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
|
|
|
*
|
2012-12-01 20:28:06 +04:00
|
|
|
* @param string|integer $key The parameter position or name.
|
|
|
|
* @param mixed $value The parameter value.
|
|
|
|
* @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)
|
2012-11-12 13:23:50 +04:00
|
|
|
* )));
|
2010-05-07 15:36:25 +04:00
|
|
|
* </code>
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2012-12-01 20:28:06 +04:00
|
|
|
* @param \Doctrine\Common\Collections\ArrayCollection|array $parameters 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
|
|
|
*
|
2012-07-20 03:17:45 +04:00
|
|
|
* @return Query\Parameter|null The value of the bound parameter.
|
2009-07-10 01:56:34 +04:00
|
|
|
*/
|
|
|
|
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.
|
2012-12-01 20:28:06 +04:00
|
|
|
*
|
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.
|
2012-12-01 20:28:06 +04:00
|
|
|
*
|
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
|
|
|
*
|
2012-12-01 20:28:06 +04:00
|
|
|
* @param string $dqlPartName
|
|
|
|
* @param Expr\Base $dqlPart
|
|
|
|
* @param bool $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)
|
|
|
|
{
|
2013-01-06 13:33:57 +04:00
|
|
|
if ($append && ($dqlPartName === "where" || $dqlPartName === "having")) {
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
"Using \$append = true does not have an effect with 'where' or 'having' ".
|
|
|
|
"parts. See QueryBuilder#andWhere() for an example for correct usage."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-02-02 01:13:32 +03:00
|
|
|
$isMultiple = is_array($this->_dqlParts[$dqlPartName])
|
|
|
|
&& !($dqlPartName == 'join' && !$append);
|
|
|
|
|
|
|
|
// Allow adding any part retrieved from self::getDQLParts().
|
|
|
|
if (is_array($dqlPart) && $dqlPartName != 'join') {
|
|
|
|
$dqlPart = reset($dqlPart);
|
|
|
|
}
|
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.
|
2012-12-01 20:28:06 +04:00
|
|
|
*
|
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
|
|
|
/**
|
2012-12-13 21:56:25 +04:00
|
|
|
* Adds a DISTINCT flag to this query.
|
2012-01-25 13:19:01 +04:00
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->distinct()
|
|
|
|
* ->from('User', 'u');
|
|
|
|
* </code>
|
|
|
|
*
|
2012-12-01 20:28:06 +04:00
|
|
|
* @param bool $flag
|
|
|
|
*
|
2012-01-25 13:19:01 +04:00
|
|
|
* @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.
|
2012-12-01 20:28:06 +04:00
|
|
|
*
|
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')
|
2014-01-03 20:28:42 +04:00
|
|
|
* ->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.
|
2012-12-01 20:28:06 +04:00
|
|
|
* @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.
|
2012-12-01 20:28:06 +04:00
|
|
|
* @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
|
|
|
/**
|
2012-12-13 21:56:25 +04:00
|
|
|
* Creates and adds a query root corresponding to the entity identified by the given alias,
|
2010-05-07 15:36:25 +04:00
|
|
|
* 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')
|
2014-01-03 20:28:42 +04:00
|
|
|
* ->from('User', 'u');
|
2010-05-07 15:36:25 +04:00
|
|
|
* </code>
|
2009-09-10 00:34:42 +04:00
|
|
|
*
|
2012-12-01 20:28:06 +04: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.
|
2012-12-01 20:28:06 +04:00
|
|
|
*
|
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
|
|
|
|
2015-02-25 22:29:10 +03:00
|
|
|
/**
|
|
|
|
* Updates a query root corresponding to an entity setting its index by. This method is intended to be used with
|
|
|
|
* EntityRepository->createQueryBuilder(), which creates the initial FROM clause and do not allow you to update it
|
|
|
|
* setting an index by.
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* $qb = $userRepository->createQueryBuilder('u')
|
|
|
|
* ->indexBy('u', 'u.id');
|
|
|
|
*
|
|
|
|
* // Is equivalent to...
|
|
|
|
*
|
|
|
|
* $qb = $em->createQueryBuilder()
|
|
|
|
* ->select('u')
|
|
|
|
* ->from('User', 'u', 'u.id');
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @param string $alias The root alias of the class.
|
|
|
|
* @param string $indexBy The index for the from.
|
|
|
|
*
|
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
|
|
|
*
|
|
|
|
* @throws Query\QueryException
|
|
|
|
*/
|
|
|
|
public function indexBy($alias, $indexBy)
|
|
|
|
{
|
|
|
|
$rootAliases = $this->getRootAliases();
|
|
|
|
|
|
|
|
if (!in_array($alias, $rootAliases)) {
|
|
|
|
throw new Query\QueryException(
|
|
|
|
sprintf('Specified root alias %s must be set before invoking indexBy().', $alias)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->_dqlParts['from'] as &$fromClause) {
|
|
|
|
if ($fromClause->getAlias() !== $alias) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fromClause = new Expr\From($fromClause->getFrom(), $fromClause->getAlias(), $indexBy);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2012-12-01 20:28:06 +04:00
|
|
|
* @param string $join The relationship to join.
|
|
|
|
* @param string $alias The alias of the join.
|
|
|
|
* @param string|null $conditionType The condition type constant. Either ON or WITH.
|
|
|
|
* @param string|null $condition The condition for the join.
|
|
|
|
* @param string|null $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');
|
|
|
|
*
|
2012-12-01 20:28:06 +04:00
|
|
|
* @param string $join The relationship to join.
|
|
|
|
* @param string $alias The alias of the join.
|
|
|
|
* @param string|null $conditionType The condition type constant. Either ON or WITH.
|
|
|
|
* @param string|null $condition The condition for the join.
|
|
|
|
* @param string|null $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
|
|
|
*
|
2012-12-01 20:28:06 +04:00
|
|
|
* @param string $join The relationship to join.
|
|
|
|
* @param string $alias The alias of the join.
|
|
|
|
* @param string|null $conditionType The condition type constant. Either ON or WITH.
|
|
|
|
* @param string|null $condition The condition for the join.
|
|
|
|
* @param string|null $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
|
|
|
*
|
2012-12-01 20:28:06 +04:00
|
|
|
* @param string $key The key/field to set.
|
2010-05-07 15:36:25 +04:00
|
|
|
* @param string $value The value, expression, placeholder, etc.
|
2012-12-01 20:28:06 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @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.
|
2012-12-01 20:28:06 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @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.
|
2012-12-01 20:28:06 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @return QueryBuilder This QueryBuilder instance.
|
2012-12-01 20:28:06 +04:00
|
|
|
*
|
2009-09-10 00:34:42 +04:00
|
|
|
* @see where()
|
|
|
|
*/
|
2014-10-17 22:17:33 +04:00
|
|
|
public function andWhere()
|
2009-07-10 18:02:06 +04:00
|
|
|
{
|
2012-03-15 09:26:06 +04:00
|
|
|
$args = func_get_args();
|
2013-09-15 21:57:30 +04:00
|
|
|
$where = $this->getDQLPart('where');
|
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
|
|
|
|
2013-01-06 13:33:57 +04:00
|
|
|
return $this->add('where', $where);
|
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
|
|
|
*
|
2012-12-01 20:28:06 +04:00
|
|
|
* @param mixed $where The WHERE statement.
|
|
|
|
*
|
|
|
|
* @return QueryBuilder
|
|
|
|
*
|
2009-09-10 00:34:42 +04:00
|
|
|
* @see where()
|
|
|
|
*/
|
2014-10-17 22:17:33 +04:00
|
|
|
public function orWhere()
|
2009-07-10 18:02:06 +04:00
|
|
|
{
|
2012-03-15 09:26:06 +04:00
|
|
|
$args = func_get_args();
|
2013-09-15 21:57:30 +04:00
|
|
|
$where = $this->getDqlPart('where');
|
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
|
|
|
|
2013-01-06 13:33:57 +04:00
|
|
|
return $this->add('where', $where);
|
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.
|
2012-12-01 20:28:06 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @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')
|
2014-01-03 20:28:42 +04:00
|
|
|
* ->groupBy('u.lastLogin')
|
|
|
|
* ->addGroupBy('u.createdAt');
|
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.
|
2012-12-01 20:28:06 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @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.
|
2012-12-01 20:28:06 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @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.
|
2012-12-01 20:28:06 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @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)
|
|
|
|
{
|
2012-03-15 09:26:06 +04:00
|
|
|
$args = func_get_args();
|
2013-09-15 21:57:30 +04:00
|
|
|
$having = $this->getDqlPart('having');
|
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.
|
2012-12-01 20:28:06 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @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)
|
|
|
|
{
|
2012-03-15 09:26:06 +04:00
|
|
|
$args = func_get_args();
|
2013-09-15 21:57:30 +04:00
|
|
|
$having = $this->getDqlPart('having');
|
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
|
|
|
*
|
2013-02-10 01:40:34 +04:00
|
|
|
* @param string|Expr\OrderBy $sort The ordering expression.
|
|
|
|
* @param string $order The ordering direction.
|
2012-12-01 20:28:06 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @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
|
|
|
*
|
2013-02-10 01:40:34 +04:00
|
|
|
* @param string|Expr\OrderBy $sort The ordering expression.
|
|
|
|
* @param string $order The ordering direction.
|
2012-12-01 20:28:06 +04:00
|
|
|
*
|
2010-05-07 15:36:25 +04:00
|
|
|
* @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
|
|
|
{
|
2013-02-10 01:40:34 +04:00
|
|
|
$orderBy = ($sort instanceof Expr\OrderBy) ? $sort : new Expr\OrderBy($sort, $order);
|
|
|
|
|
|
|
|
return $this->add('orderBy', $orderBy, true);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2012-08-19 21:58:04 +04:00
|
|
|
/**
|
2012-12-13 21:56:25 +04:00
|
|
|
* Adds criteria to the query.
|
|
|
|
*
|
|
|
|
* Adds where expressions with AND operator.
|
|
|
|
* Adds orderings.
|
|
|
|
* Overrides firstResult and maxResults if they're set.
|
2012-08-19 21:58:04 +04:00
|
|
|
*
|
|
|
|
* @param Criteria $criteria
|
|
|
|
* @return QueryBuilder
|
2015-01-12 17:28:55 +03:00
|
|
|
* @throws Query\QueryException
|
2012-08-19 21:58:04 +04:00
|
|
|
*/
|
|
|
|
public function addCriteria(Criteria $criteria)
|
|
|
|
{
|
2014-12-06 17:40:03 +03:00
|
|
|
$allAliases = $this->getAllAliases();
|
|
|
|
if ( ! isset($allAliases[0])) {
|
2015-01-12 17:28:55 +03:00
|
|
|
throw new Query\QueryException('No aliases are set before invoking addCriteria().');
|
2014-12-06 17:40:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$visitor = new QueryExpressionVisitor($this->getAllAliases());
|
2012-08-19 21:58:04 +04:00
|
|
|
|
|
|
|
if ($whereExpression = $criteria->getWhereExpression()) {
|
|
|
|
$this->andWhere($visitor->dispatch($whereExpression));
|
|
|
|
foreach ($visitor->getParameters() as $parameter) {
|
|
|
|
$this->parameters->add($parameter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($criteria->getOrderings()) {
|
|
|
|
foreach ($criteria->getOrderings() as $sort => $order) {
|
2014-12-06 17:40:03 +03:00
|
|
|
|
|
|
|
$hasValidAlias = false;
|
|
|
|
foreach($allAliases as $alias) {
|
|
|
|
if(strpos($sort . '.', $alias . '.') === 0) {
|
|
|
|
$hasValidAlias = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$hasValidAlias) {
|
|
|
|
$sort = $allAliases[0] . '.' . $sort;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->addOrderBy($sort, $order);
|
2012-08-19 21:58:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-20 16:27:52 +04:00
|
|
|
// Overwrite limits only if they was set in criteria
|
2012-08-20 19:52:23 +04:00
|
|
|
if (($firstResult = $criteria->getFirstResult()) !== null) {
|
2012-08-20 16:27:52 +04:00
|
|
|
$this->setFirstResult($firstResult);
|
|
|
|
}
|
|
|
|
if (($maxResults = $criteria->getMaxResults()) !== null) {
|
|
|
|
$this->setMaxResults($maxResults);
|
|
|
|
}
|
2012-08-19 21:58:04 +04:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2009-10-04 23:35:47 +04:00
|
|
|
/**
|
2012-12-13 21:56:25 +04:00
|
|
|
* Gets a query part by its name.
|
2009-10-04 23:35:47 +04:00
|
|
|
*
|
|
|
|
* @param string $queryPartName
|
2012-12-01 20:28:06 +04:00
|
|
|
*
|
2009-10-04 23:35:47 +04:00
|
|
|
* @return mixed $queryPart
|
2012-12-01 20:28:06 +04:00
|
|
|
*
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-13 21:56:25 +04:00
|
|
|
* Gets all query parts.
|
2009-10-04 23:35:47 +04:00
|
|
|
*
|
|
|
|
* @return array $dqlParts
|
2012-12-01 20:28:06 +04:00
|
|
|
*
|
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;
|
|
|
|
}
|
|
|
|
|
2012-12-01 20:28:06 +04:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2012-12-01 20:28:06 +04:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2012-12-01 20:28:06 +04:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2012-12-01 20:28:06 +04:00
|
|
|
/**
|
|
|
|
* @param string $queryPartName
|
|
|
|
* @param array $options
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
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
|
|
|
/**
|
2012-12-13 21:56:25 +04:00
|
|
|
* Resets DQL parts.
|
2010-09-24 00:32:23 +04:00
|
|
|
*
|
2012-12-01 20:28:06 +04:00
|
|
|
* @param array|null $parts
|
|
|
|
*
|
2010-09-24 00:32:23 +04:00
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-13 21:56:25 +04:00
|
|
|
* Resets single DQL part.
|
2010-09-24 00:32:23 +04:00
|
|
|
*
|
|
|
|
* @param string $part
|
2012-12-01 20:28:06 +04:00
|
|
|
*
|
|
|
|
* @return QueryBuilder
|
2010-09-24 00:32:23 +04:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
2012-12-13 21:56:25 +04:00
|
|
|
* Deep clones all expression objects in the DQL parts.
|
2010-11-15 23:32:38 +03:00
|
|
|
*
|
|
|
|
* @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-07-20 03:17:45 +04:00
|
|
|
|
|
|
|
$parameters = array();
|
|
|
|
|
|
|
|
foreach ($this->parameters as $parameter) {
|
|
|
|
$parameters[] = clone $parameter;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->parameters = new ArrayCollection($parameters);
|
2010-11-15 23:32:38 +03:00
|
|
|
}
|
2012-01-25 13:19:01 +04:00
|
|
|
}
|