class Doctrine_Query extends Doctrine_Query_Abstract implements Countable (View source)

Doctrine_Query A Doctrine_Query object represents a DQL query. It is used to query databases for data in an object-oriented fashion. A DQL query understands relations and inheritance and is dbms independant.

Constants

SELECT

constant for SELECT queries

DELETE

constant for DELETE queries

UPDATE

constant for UPDATE queries

INSERT

constant for INSERT queries

CREATE

constant for CREATE queries

STATE_CLEAN

A query object is in CLEAN state when it has NO unparsed/unprocessed DQL parts.

STATE_DIRTY

A query object is in state DIRTY when it has DQL parts that have not yet been parsed/processed.

STATE_DIRECT

A query is in DIRECT state when .

.. ?

STATE_LOCKED

A query object is on LOCKED state when .

.. ?

Properties

protected array $_tableAliasMap from Doctrine_Query_Abstract
protected Doctrine_View $_view from Doctrine_Query_Abstract
protected integer $_state from Doctrine_Query_Abstract
protected array $_params from Doctrine_Query_Abstract
protected array $_execParams from Doctrine_Query_Abstract
protected Doctrine_Cache_Interface $_resultCache from Doctrine_Query_Abstract
protected string $_resultCacheHash from Doctrine_Query_Abstract
protected boolean $_expireResultCache from Doctrine_Query_Abstract
protected $_resultCacheTTL from Doctrine_Query_Abstract
protected Doctrine_Cache_Interface $_queryCache from Doctrine_Query_Abstract
protected $_expireQueryCache from Doctrine_Query_Abstract
protected $_queryCacheTTL from Doctrine_Query_Abstract
protected Doctrine_Connection $_conn from Doctrine_Query_Abstract
protected bool $_passedConn from Doctrine_Query_Abstract
protected array $_sqlParts from Doctrine_Query_Abstract
protected array $_dqlParts from Doctrine_Query_Abstract
protected array $_queryComponents from Doctrine_Query_Abstract
protected string $_rootAlias Stores the root DQL alias from Doctrine_Query_Abstract
protected integer $_type from Doctrine_Query_Abstract
protected Doctrine_Hydrator $_hydrator from Doctrine_Query_Abstract
protected Doctrine_Query_Tokenizer $_tokenizer from Doctrine_Query_Abstract
protected Doctrine_Query_Parser $_parser from Doctrine_Query_Abstract
protected array $_tableAliasSeeds from Doctrine_Query_Abstract
protected array $_options from Doctrine_Query_Abstract
protected boolean $_isLimitSubqueryUsed from Doctrine_Query_Abstract
protected array $_components from Doctrine_Query_Abstract
protected bool $_preQueried from Doctrine_Query_Abstract
protected bool $disableLimitSubquery Fix for http://www.doctrine-project.org/jira/browse/DC-701 from Doctrine_Query_Abstract
static protected array $_keywords
protected array $_subqueryAliases
protected array $_aggregateAliasMap
protected array $_pendingAggregates
protected $_needsSubquery
protected $_isSubquery
protected array $_neededTables
protected array $_pendingSubqueries
protected array $_pendingFields
protected array $_parsers
protected array $_pendingJoinConditions
protected array $_expressionMap
protected string $_sql

Methods

__construct(Doctrine_Connection $connection = null, Doctrine_Hydrator_Abstract $hydrator = null)

Constructor.

void
setConnection(Doctrine_Connection $connection)

Set the connection this query object should use

setOption(string $name, string $value)

setOption

boolean
hasSqlTableAlias($sqlTableAlias)

hasSqlTableAlias whether or not this object has given tableAlias

array
getTableAliasMap()

getTableAliasMap returns all table aliases

string
getDql()

getDql returns the DQL query that is represented by this query object.

mixed
getSqlQueryPart($part)

getSqlQueryPart gets an SQL query part from the SQL query part array

setSqlQueryPart(string $name, string $part)

setSqlQueryPart sets an SQL query part in the SQL query part array

addSqlQueryPart(string $name, string $part)

addSqlQueryPart adds an SQL query part to the SQL query part array

removeSqlQueryPart(string $name)

removeSqlQueryPart removes a query part from the query part array

removeDqlQueryPart(string $name)

removeDqlQueryPart removes a dql query part from the dql query part array

array
getParams()

Get raw array of parameters for query and all parts.

void
getFlattenedParams(array $params = array())

Get flattened array of parameters for query.

array
getInternalParams($params = array())

getInternalParams

setParams(array $params = array())

setParams

array
getCountQueryParams($params = array())

getCountQueryParams Retrieves the parameters for count query

fixArrayParameterValues($params = array())

No description

void
setView(Doctrine_View $view)

setView sets a database view this query object uses this method should only be called internally by doctrine

getView()

getView returns the view associated with this query object (if any)

boolean
isLimitSubqueryUsed()

limitSubqueryUsed

string
getInheritanceCondition($componentAlias)

Returns the inheritance condition for the passed componentAlias If no component alias is specified it defaults to the root component

string
getSqlTableAlias(string $componentAlias, string $tableName = null)

getSqlTableAlias some database such as Oracle need the identifier lengths to be < ~30 chars hence Doctrine creates as short identifier aliases as possible

string
generateNewSqlTableAlias($oldAlias)

generateNewSqlTableAlias generates a new alias from given table alias

integer
getSqlTableAliasSeed($sqlTableAlias)

getSqlTableAliasSeed returns the alias seed for given table alias

boolean
hasAliasDeclaration(string $componentAlias)

hasAliasDeclaration whether or not this object has a declaration for given component alias

array
getQueryComponent(string $componentAlias)

getQueryComponent get the declaration for given component alias

copySubqueryInfo(Doctrine_Query_Abstract $query)

copySubqueryInfo copy aliases from another Hydrate object

array
getRootAlias()

getRootAlias returns the alias of the root component

array
getRootDeclaration()

getRootDeclaration returns the root declaration

getRoot()

getRoot returns the root component for this object

string
generateSqlTableAlias(string $componentAlias, string $tableName)

generateSqlTableAlias generates a table alias from given table name and associates it with given component alias

string
getComponentAlias(string $sqlTableAlias)

getComponentAlias get component alias associated with given table alias

string
calculateQueryCacheHash()

calculateQueryCacheHash calculate hash key for query cache

string
calculateResultCacheHash(array $params = array())

calculateResultCacheHash calculate hash key for result cache

string
getResultCacheHash(array $params = array())

Get the result cache hash/key. Returns key set with useResultCache() or generates a unique key from the query automatically.

_execute(array $params)

_execute

execute($params = array(), $hydrationMode = null)

execute executes the query and populates the data set

free()

Frees the resources used by the query object. It especially breaks a cyclic reference between the query object and it's parsers. This enables PHP's current GC to reclaim the memory.

array
_getDqlCallback()

Get the dql call back for this query

void
_preQuery($params = array())

Pre query method which invokes the pre*Query() methods on the model instance or any attached record listeners

array
_getDqlCallbackComponents(array $params = array())

Returns an array of components to execute the query callbacks for

void
preQuery()

Blank hook methods which can be implemented in Doctrine_Query child classes

array
_constructQueryFromCache(string $cached)

Constructs the query from the cached form.

string
getCachedForm($customComponent = null)

getCachedForm returns the cached form of this query for given resultSet

addSelect(string $select)

Adds fields or aliased functions.

addSqlTableAlias($sqlTableAlias, $componentAlias)

addSqlTableAlias adds an SQL table alias and associates it a component alias

addFrom(string $from)

addFrom adds fields to the FROM part of the query

addWhere($where, $params = array())

Alias for see andWhere().

andWhere(string $where, mixed $params = array())

Adds conditions to the WHERE part of the query.

orWhere(string $where, mixed $params = array())

Adds conditions to the WHERE part of the query $q->orWhere('u.role = ?', 'admin');

whereIn(string $expr, mixed $params = array(), boolean $not = false)

Adds IN condition to the query WHERE part. Alias to see andWhereIn().

andWhereIn(string $expr, mixed $params = array(), boolean $not = false)

Adds IN condition to the query WHERE part $q->whereIn('u.id', array(10, 23, 44));

orWhereIn(string $expr, mixed $params = array(), boolean $not = false)

Adds IN condition to the query WHERE part, appending it with an OR operator.

_processWhereIn($expr, $params = array(), $not = false)

No description

whereNotIn(string $expr, mixed $params = array())

Adds NOT IN condition to the query WHERE part.

andWhereNotIn(string $expr, mixed $params = array())

Adds NOT IN condition to the query WHERE part Alias for see whereNotIn().

orWhereNotIn(string $expr, mixed $params = array())

Adds NOT IN condition to the query WHERE part

addGroupBy(string $groupby)

Adds fields to the GROUP BY part of the query.

addHaving(string $having, mixed $params = array())

Adds conditions to the HAVING part of the query.

addOrderBy(string $orderby)

addOrderBy adds fields to the ORDER BY part of the query

select(string $select = null)

select sets the SELECT part of the query

distinct(bool $flag = true)

distinct Makes the query SELECT DISTINCT.

forUpdate(bool $flag = true)

forUpdate Makes the query SELECT FOR UPDATE.

delete($from = null)

delete sets the query type to DELETE

update($from = null)

update sets the UPDATE part of the query

set($key, $value = null, $params = null)

set sets the SET part of the query

from(string $from)

from sets the FROM part of the query $q->from('User u');

innerJoin($join, $params = array())

innerJoin appends an INNER JOIN to the FROM part of the query

leftJoin($join, $params = array())

leftJoin appends a LEFT JOIN to the FROM part of the query

groupBy(string $groupby)

groupBy sets the GROUP BY part of the query

where($where, $params = array())

where sets the WHERE part of the query

having(string $having, mixed $params = array())

having sets the HAVING part of the query

orderBy(string $orderby)

Sets the ORDER BY part of the query.

limit(integer $limit)

limit sets the Query query limit

offset(integer $offset)

offset sets the Query query offset

void
clear()

Clears all the sql parts.

setHydrationMode($hydrationMode)

No description

getQueryComponents()

Gets the components of this query.

array
getSqlParts()

Return the SQL parts.

integer
getType()

getType

useResultCache(Doctrine_Cache_Interface|bool $driver = true, integer $timeToLive = null, string $resultCacheHash = null)

useResultCache

void
setResultCacheHash(string $resultCacheHash)

Set the result cache hash to be used for storing the results in the cache driver

void
clearResultCache()

Clear the result cache entry for this query

useQueryCache(Doctrine_Cache_Interface|bool $driver = true, integer $timeToLive = null)

useQueryCache

expireResultCache(boolean $expire = true)

expireCache

expireQueryCache(boolean $expire = true)

expireQueryCache

setResultCacheLifeSpan(integer $timeToLive)

setResultCacheLifeSpan

integer
getResultCacheLifeSpan()

Gets the life span of the result cache in seconds.

setQueryCacheLifeSpan(integer $timeToLive)

setQueryCacheLifeSpan

integer
getQueryCacheLifeSpan()

Gets the life span of the query cache the Query object is using.

getResultCacheDriver()

getResultCacheDriver returns the cache driver used for caching result sets

getQueryCacheDriver()

getQueryCacheDriver returns the cache driver used for caching queries

boolean
_hasDqlQueryPart(string $queryPartName)

Checks if there's at least one DQL part defined to the internal parts collection.

_addDqlQueryPart(string $queryPartName, string $queryPart, boolean $append = false)

Adds a DQL part to the internal parts collection.

_processDqlQueryPart(string $queryPartName, array $queryParts)

_processDqlQueryPart parses given query part

_getParser($name)

_getParser parser lazy-loader

getSqlQuery(array $params = array(), bool $limitSubquery = true)

builds the sql query from the given parameters and applies things such as column aggregation inheritance and limit subqueries if needed

parseDqlQuery(string $query, boolean $clear = true)

DQL PARSER parses a DQL query first splits the query in parts and then uses individual parsers for each part

string
__toString()

toString magic call this method is automatically called when Doctrine_Query object is trying to be used as a string So, it it converted into its DQL correspondant

boolean
getDisableLimitSubquery()

Gets the disableLimitSubquery property.

setDisableLimitSubquery(boolean $disableLimitSubquery)

Allows you to set the disableLimitSubquery property -- setting this to true will restrict the query object from using the limit sub query method of tranversing many relationships.

static Doctrine_Query
create(Doctrine_Connection $conn = null, string $class = null)

create returns a new Doctrine_Query object

reset()

Resets the query to the state just after it has been instantiated.

Doctrine_Hydrate
createSubquery()

createSubquery creates a subquery

addPendingJoinCondition(string $componentAlias, string $joinCondition)

addPendingJoinCondition

array
fetchArray(string $params = array())

fetchArray Convenience method to execute using array fetching as hydration mode.

mixed
fetchOne(string $params = array(), int $hydrationMode = null)

fetchOne Convenience method to execute the query and return the first item of the collection.

isSubquery(boolean $bool = null)

isSubquery if $bool parameter is set this method sets the value of Doctrine_Query::$isSubquery. If this value is set to true the query object will not load the primary key fields of the selected components.

string
getSqlAggregateAlias(string $dqlAlias)

getSqlAggregateAlias

boolean
hasSqlAggregateAlias(string $dqlAlias)

Check if a dql alias has a sql aggregate alias

adjustProcessedParam($index)

Adjust the processed param index for "foo.bar IN ?" support

array
getDqlPart(string $queryPart)

Retrieves a specific DQL query part.

boolean
contains(string $dql)

contains

string
processPendingFields(string $componentAlias)

processPendingFields the fields in SELECT clause cannot be parsed until the components in FROM clause are parsed, hence this method is called everytime a specific component is being parsed. For instance, the wildcard '*' is expanded in the list of columns.

string
parseSelectField(string $field)

Parses a nested field $q->parseSelectField('u.Phonenumber.value');

string
getExpressionOwner(string $expr)

getExpressionOwner returns the component alias for owner of given expression

parseSelect(string $dql)

parseSelect parses the query select part and adds selected fields to pendingFields array

string
parseClause($clause)

parseClause parses given DQL clause

parseIdentifierReference($expr)

No description

parseFunctionExpression($expr, $parseCallback = null)

No description

parseSubquery($subquery)

No description

void
processPendingSubqueries()

processPendingSubqueries processes pending subqueries

void
processPendingAggregates()

processPendingAggregates processes pending aggregate values for given component alias

string
_buildSqlQueryBase()

_buildSqlQueryBase returns the base of the generated sql query On mysql driver special strategy has to be used for DELETE statements (where is this special strategy??)

string
_buildSqlFromPart($ignorePending = false)

_buildSqlFromPart builds the from part of the query and returns it

Processed
_processPendingJoinConditions(string $alias)

Processes the pending join conditions, used for dynamically add conditions to root component/joined components without interfering in the main dql handling.

string
buildSqlQuery(bool $limitSubquery = true)

Build the SQL query from the DQL

string
getLimitSubquery()

getLimitSubquery this is method is used by the record limit algorithm

load(string $path, boolean $loadFields = true)

No description

buildSimpleRelationSql(Doctrine_Relation $relation, $foreignAlias, $localAlias, $overrideJoin, $join)

No description

buildIndexBy($componentAlias, $mapWith = null)

No description

buildAssociativeRelationSql(Doctrine_Relation $relation, $assocAlias, $foreignAlias, $localAlias)

No description

loadRoot(string $name, string $componentAlias)

loadRoot

string
buildInheritanceJoinSql(string $name, string $componentAlias)

No description

string
getCountSqlQuery()

Get count sql query for this Doctrine_Query instance.

integer
count(array $params = array())

Fetches the count of the query.

mixed
query(string $query, array $params = array(), int $hydrationMode = null)

Queries the database with DQL (Doctrine Query Language).

copy(Doctrine_Query $query = null)

Copies a Doctrine_Query object.

void
__clone()

Magic method called after cloning process.

_killReference(string $key)

Kill the reference for the passed class property.

Details

__construct(Doctrine_Connection $connection = null, Doctrine_Hydrator_Abstract $hydrator = null)

Constructor.

Parameters

Doctrine_Connection $connection The connection object the query will use.
Doctrine_Hydrator_Abstract $hydrator The hydrator that will be used for generating result sets.

void setConnection(Doctrine_Connection $connection)

Set the connection this query object should use

Parameters

Doctrine_Connection $connection

Return Value

void

Doctrine_Query setOption(string $name, string $value)

setOption

Parameters

string $name option name
string $value option value

Return Value

Doctrine_Query this object

boolean hasSqlTableAlias($sqlTableAlias)

hasSqlTableAlias whether or not this object has given tableAlias

Parameters

$sqlTableAlias

Return Value

boolean true if this object has given alias, otherwise false

array getTableAliasMap()

getTableAliasMap returns all table aliases

Return Value

array table aliases as an array

string getDql()

getDql returns the DQL query that is represented by this query object.

the query is built from $_dqlParts

Return Value

string the DQL query

mixed getSqlQueryPart($part)

getSqlQueryPart gets an SQL query part from the SQL query part array

Parameters

$part

Return Value

mixed this object

Exceptions

Doctrine_Query_Exception if trying to set unknown query part

Doctrine_Query setSqlQueryPart(string $name, string $part)

setSqlQueryPart sets an SQL query part in the SQL query part array

Parameters

string $name the name of the query part to be set
string $part query part string

Return Value

Doctrine_Query this object

Exceptions

Doctrine_Query_Exception if trying to set unknown query part

Doctrine_Query addSqlQueryPart(string $name, string $part)

addSqlQueryPart adds an SQL query part to the SQL query part array

Parameters

string $name the name of the query part to be added
string $part query part string

Return Value

Doctrine_Query this object

Exceptions

Doctrine_Query_Exception if trying to add unknown query part

Doctrine_Query removeSqlQueryPart(string $name)

removeSqlQueryPart removes a query part from the query part array

Parameters

string $name the name of the query part to be removed

Return Value

Doctrine_Query this object

Exceptions

Doctrine_Query_Exception if trying to remove unknown query part

Doctrine_Query removeDqlQueryPart(string $name)

removeDqlQueryPart removes a dql query part from the dql query part array

Parameters

string $name the name of the query part to be removed

Return Value

Doctrine_Query this object

Exceptions

Doctrine_Query_Exception if trying to remove unknown query part

array getParams()

Get raw array of parameters for query and all parts.

Return Value

array $params

void getFlattenedParams(array $params = array())

Get flattened array of parameters for query.

Used internally and used to pass flat array of params to the database.

Parameters

array $params

Return Value

void

array getInternalParams($params = array())

getInternalParams

Parameters

$params

Return Value

array

setParams(array $params = array())

setParams

Parameters

array $params

array getCountQueryParams($params = array())

getCountQueryParams Retrieves the parameters for count query

Parameters

$params

Return Value

array Parameters array

fixArrayParameterValues($params = array())

Parameters

$params

void setView(Doctrine_View $view)

setView sets a database view this query object uses this method should only be called internally by doctrine

Parameters

Doctrine_View $view database view

Return Value

void

Doctrine_View getView()

getView returns the view associated with this query object (if any)

Return Value

Doctrine_View the view associated with this query object

boolean isLimitSubqueryUsed()

limitSubqueryUsed

Return Value

boolean

string getInheritanceCondition($componentAlias)

Returns the inheritance condition for the passed componentAlias If no component alias is specified it defaults to the root component

This function is used to append a SQL condition to models which have inheritance mapping The condition is applied to the FROM component in the WHERE, but the condition is applied to JOINS in the ON condition and not the WHERE

Parameters

$componentAlias

Return Value

string $str SQL condition string

string getSqlTableAlias(string $componentAlias, string $tableName = null)

getSqlTableAlias some database such as Oracle need the identifier lengths to be < ~30 chars hence Doctrine creates as short identifier aliases as possible

this method is used for the creation of short table aliases, its also smart enough to check if an alias already exists for given component (componentAlias)

Parameters

string $componentAlias the alias for the query component to search table alias for
string $tableName the table name from which the table alias is being created

Return Value

string the generated / fetched short alias

string generateNewSqlTableAlias($oldAlias)

generateNewSqlTableAlias generates a new alias from given table alias

Parameters

$oldAlias

Return Value

string the created table alias

integer getSqlTableAliasSeed($sqlTableAlias)

getSqlTableAliasSeed returns the alias seed for given table alias

Parameters

$sqlTableAlias

Return Value

integer table alias seed

boolean hasAliasDeclaration(string $componentAlias)

hasAliasDeclaration whether or not this object has a declaration for given component alias

Parameters

string $componentAlias the component alias the retrieve the declaration from

Return Value

boolean

array getQueryComponent(string $componentAlias)

getQueryComponent get the declaration for given component alias

Parameters

string $componentAlias the component alias the retrieve the declaration from

Return Value

array the alias declaration

Doctrine_Query copySubqueryInfo(Doctrine_Query_Abstract $query)

copySubqueryInfo copy aliases from another Hydrate object

this method is needed by DQL subqueries which need the aliases of the parent query

Parameters

Doctrine_Query_Abstract $query the query object from which the aliases are copied from

Return Value

Doctrine_Query this object

array getRootAlias()

getRootAlias returns the alias of the root component

Return Value

array

array getRootDeclaration()

getRootDeclaration returns the root declaration

Return Value

array

Doctrine_Table getRoot()

getRoot returns the root component for this object

Return Value

Doctrine_Table root components table

string generateSqlTableAlias(string $componentAlias, string $tableName)

generateSqlTableAlias generates a table alias from given table name and associates it with given component alias

Parameters

string $componentAlias the component alias to be associated with generated table alias
string $tableName the table name from which to generate the table alias

Return Value

string the generated table alias

string getComponentAlias(string $sqlTableAlias)

getComponentAlias get component alias associated with given table alias

Parameters

string $sqlTableAlias the SQL table alias that identifies the component alias

Return Value

string component alias

string calculateQueryCacheHash()

calculateQueryCacheHash calculate hash key for query cache

Return Value

string the hash

string calculateResultCacheHash(array $params = array())

calculateResultCacheHash calculate hash key for result cache

Parameters

array $params

Return Value

string the hash

string getResultCacheHash(array $params = array())

Get the result cache hash/key. Returns key set with useResultCache() or generates a unique key from the query automatically.

Parameters

array $params

Return Value

string $hash

protected PDOStatement _execute(array $params)

_execute

Parameters

array $params

Return Value

PDOStatement The executed PDOStatement.

Doctrine_Collection execute($params = array(), $hydrationMode = null)

execute executes the query and populates the data set

Parameters

$params
$hydrationMode

Return Value

Doctrine_Collection the root collection

free()

Frees the resources used by the query object. It especially breaks a cyclic reference between the query object and it's parsers. This enables PHP's current GC to reclaim the memory.

This method can therefore be used to reduce memory usage when creating a lot of query objects during a request.

protected array _getDqlCallback()

Get the dql call back for this query

Return Value

array $callback

protected void _preQuery($params = array())

Pre query method which invokes the pre*Query() methods on the model instance or any attached record listeners

Parameters

$params

Return Value

void

protected array _getDqlCallbackComponents(array $params = array())

Returns an array of components to execute the query callbacks for

Parameters

array $params

Return Value

array $components

void preQuery()

Blank hook methods which can be implemented in Doctrine_Query child classes

Return Value

void

protected array _constructQueryFromCache(string $cached)

Constructs the query from the cached form.

Parameters

string $cached The cached query, in a serialized form.

Return Value

array The custom component that was cached together with the essential query data. This can be either a result set (result caching) or an SQL query string (query caching).

string getCachedForm($customComponent = null)

getCachedForm returns the cached form of this query for given resultSet

Parameters

$customComponent

Return Value

string serialized string representation of this query

Doctrine_Query addSelect(string $select)

Adds fields or aliased functions.

This method adds fields or dbms functions to the SELECT query part. $query->addSelect('COUNT(p.id) as num_phonenumbers');

Parameters

string $select Query SELECT part

Return Value

Doctrine_Query

Doctrine_Query_Abstract addSqlTableAlias($sqlTableAlias, $componentAlias)

addSqlTableAlias adds an SQL table alias and associates it a component alias

Parameters

$sqlTableAlias
$componentAlias

Return Value

Doctrine_Query_Abstract

Doctrine_Query addFrom(string $from)

addFrom adds fields to the FROM part of the query

Parameters

string $from Query FROM part

Return Value

Doctrine_Query

Doctrine_Query addWhere($where, $params = array())

Alias for see andWhere().

Parameters

$where
$params

Return Value

Doctrine_Query this object

Doctrine_Query andWhere(string $where, mixed $params = array())

Adds conditions to the WHERE part of the query.

$q->andWhere('u.birthDate > ?', '1975-01-01');

Parameters

string $where Query WHERE part
mixed $params An array of parameters or a simple scalar

Return Value

Doctrine_Query

Doctrine_Query orWhere(string $where, mixed $params = array())

Adds conditions to the WHERE part of the query $q->orWhere('u.role = ?', 'admin');

Parameters

string $where Query WHERE part
mixed $params An array of parameters or a simple scalar

Return Value

Doctrine_Query

Doctrine_Query whereIn(string $expr, mixed $params = array(), boolean $not = false)

Adds IN condition to the query WHERE part. Alias to see andWhereIn().

Parameters

string $expr the operand of the IN
mixed $params an array of parameters or a simple scalar
boolean $not whether or not to use NOT in front of IN

Return Value

Doctrine_Query

Doctrine_Query andWhereIn(string $expr, mixed $params = array(), boolean $not = false)

Adds IN condition to the query WHERE part $q->whereIn('u.id', array(10, 23, 44));

Parameters

string $expr The operand of the IN
mixed $params An array of parameters or a simple scalar
boolean $not Whether or not to use NOT in front of IN. Defaults to false (simple IN clause)

Return Value

Doctrine_Query this object.

Doctrine_Query orWhereIn(string $expr, mixed $params = array(), boolean $not = false)

Adds IN condition to the query WHERE part, appending it with an OR operator.

$q->orWhereIn('u.id', array(10, 23)) ->orWhereIn('u.id', 44); // will select all record with id equal to 10, 23 or 44

Parameters

string $expr The operand of the IN
mixed $params An array of parameters or a simple scalar
boolean $not Whether or not to use NOT in front of IN

Return Value

Doctrine_Query

protected _processWhereIn($expr, $params = array(), $not = false)

Parameters

$expr
$params
$not

Doctrine_Query whereNotIn(string $expr, mixed $params = array())

Adds NOT IN condition to the query WHERE part.

$q->whereNotIn('u.id', array(10, 20)); // will exclude users with id 10 and 20 from the select

Parameters

string $expr the operand of the NOT IN
mixed $params an array of parameters or a simple scalar

Return Value

Doctrine_Query this object

Doctrine_Query andWhereNotIn(string $expr, mixed $params = array())

Adds NOT IN condition to the query WHERE part Alias for see whereNotIn().

Parameters

string $expr The operand of the NOT IN
mixed $params An array of parameters or a simple scalar

Return Value

Doctrine_Query

Doctrine_Query orWhereNotIn(string $expr, mixed $params = array())

Adds NOT IN condition to the query WHERE part

Parameters

string $expr The operand of the NOT IN
mixed $params An array of parameters or a simple scalar

Return Value

Doctrine_Query

Doctrine_Query addGroupBy(string $groupby)

Adds fields to the GROUP BY part of the query.

$q->groupBy('u.id');

Parameters

string $groupby Query GROUP BY part

Return Value

Doctrine_Query

Doctrine_Query addHaving(string $having, mixed $params = array())

Adds conditions to the HAVING part of the query.

This methods add HAVING clauses. These clauses are used to narrow the results by operating on aggregated values. $q->having('num_phonenumbers > ?', 1);

Parameters

string $having Query HAVING part
mixed $params an array of parameters or a simple scalar

Return Value

Doctrine_Query

Doctrine_Query addOrderBy(string $orderby)

addOrderBy adds fields to the ORDER BY part of the query

Parameters

string $orderby Query ORDER BY part

Return Value

Doctrine_Query

Doctrine_Query select(string $select = null)

select sets the SELECT part of the query

Parameters

string $select Query SELECT part

Return Value

Doctrine_Query

Doctrine_Query distinct(bool $flag = true)

distinct Makes the query SELECT DISTINCT.

$q->distinct();

Parameters

bool $flag Whether or not the SELECT is DISTINCT (default true).

Return Value

Doctrine_Query

Doctrine_Query forUpdate(bool $flag = true)

forUpdate Makes the query SELECT FOR UPDATE.

Parameters

bool $flag Whether or not the SELECT is FOR UPDATE (default true).

Return Value

Doctrine_Query

Doctrine_Query delete($from = null)

delete sets the query type to DELETE

Parameters

$from

Return Value

Doctrine_Query

Doctrine_Query update($from = null)

update sets the UPDATE part of the query

Parameters

$from

Return Value

Doctrine_Query

Doctrine_Query set($key, $value = null, $params = null)

set sets the SET part of the query

Parameters

$key
$value
$params

Return Value

Doctrine_Query

Doctrine_Query from(string $from)

from sets the FROM part of the query $q->from('User u');

Parameters

string $from Query FROM part

Return Value

Doctrine_Query

Doctrine_Query innerJoin($join, $params = array())

innerJoin appends an INNER JOIN to the FROM part of the query

Parameters

$join
$params

Return Value

Doctrine_Query

Doctrine_Query leftJoin($join, $params = array())

leftJoin appends a LEFT JOIN to the FROM part of the query

Parameters

$join
$params

Return Value

Doctrine_Query

Doctrine_Query groupBy(string $groupby)

groupBy sets the GROUP BY part of the query

Parameters

string $groupby Query GROUP BY part

Return Value

Doctrine_Query

Doctrine_Query where($where, $params = array())

where sets the WHERE part of the query

Parameters

$where
$params

Return Value

Doctrine_Query

Doctrine_Query having(string $having, mixed $params = array())

having sets the HAVING part of the query

Parameters

string $having Query HAVING part
mixed $params an array of parameters or a simple scalar

Return Value

Doctrine_Query

Doctrine_Query orderBy(string $orderby)

Sets the ORDER BY part of the query.

$q->orderBy('u.name'); $query->orderBy('u.birthDate DESC');

Parameters

string $orderby Query ORDER BY part

Return Value

Doctrine_Query

Doctrine_Query limit(integer $limit)

limit sets the Query query limit

Parameters

integer $limit limit to be used for limiting the query results

Return Value

Doctrine_Query

Doctrine_Query offset(integer $offset)

offset sets the Query query offset

Parameters

integer $offset offset to be used for paginating the query

Return Value

Doctrine_Query

protected void clear()

Clears all the sql parts.

Return Value

void

setHydrationMode($hydrationMode)

Parameters

$hydrationMode

getQueryComponents()

Gets the components of this query.

array getSqlParts()

Return the SQL parts.

Return Value

array The parts

integer getType()

getType

returns the type of this query object by default the type is Doctrine_Query_Abstract::SELECT but if update() or delete() are being called the type is Doctrine_Query_Abstract::UPDATE and Doctrine_Query_Abstract::DELETE, respectively

Return Value

integer return the query type

See also

Doctrine_Query_Abstract::SELECT
Doctrine_Query_Abstract::UPDATE
Doctrine_Query_Abstract::DELETE

Doctrine_Query useResultCache(Doctrine_Cache_Interface|bool $driver = true, integer $timeToLive = null, string $resultCacheHash = null)

useResultCache

Parameters

Doctrine_Cache_Interface|bool $driver cache driver
integer $timeToLive how long the cache entry is valid
string $resultCacheHash The key to use for storing the queries result cache entry

Return Value

Doctrine_Query this object

void setResultCacheHash(string $resultCacheHash)

Set the result cache hash to be used for storing the results in the cache driver

Parameters

string $resultCacheHash

Return Value

void

void clearResultCache()

Clear the result cache entry for this query

Return Value

void

Doctrine_Query useQueryCache(Doctrine_Cache_Interface|bool $driver = true, integer $timeToLive = null)

useQueryCache

Parameters

Doctrine_Cache_Interface|bool $driver cache driver
integer $timeToLive how long the cache entry is valid

Return Value

Doctrine_Query this object

Doctrine_Query expireResultCache(boolean $expire = true)

expireCache

Parameters

boolean $expire whether or not to force cache expiration

Return Value

Doctrine_Query this object

Doctrine_Query expireQueryCache(boolean $expire = true)

expireQueryCache

Parameters

boolean $expire whether or not to force cache expiration

Return Value

Doctrine_Query this object

Doctrine_Query setResultCacheLifeSpan(integer $timeToLive)

setResultCacheLifeSpan

Parameters

integer $timeToLive how long the cache entry is valid (in seconds)

Return Value

Doctrine_Query this object

integer getResultCacheLifeSpan()

Gets the life span of the result cache in seconds.

Return Value

integer

Doctrine_Query setQueryCacheLifeSpan(integer $timeToLive)

setQueryCacheLifeSpan

Parameters

integer $timeToLive how long the cache entry is valid

Return Value

Doctrine_Query this object

integer getQueryCacheLifeSpan()

Gets the life span of the query cache the Query object is using.

Return Value

integer The life span in seconds.

Doctrine_Cache_Interface|boolean|null getResultCacheDriver()

getResultCacheDriver returns the cache driver used for caching result sets

Return Value

Doctrine_Cache_Interface|boolean|null cache driver

Doctrine_Cache_Interface|boolean|null getQueryCacheDriver()

getQueryCacheDriver returns the cache driver used for caching queries

Return Value

Doctrine_Cache_Interface|boolean|null cache driver

Doctrine_Connection getConnection()

getConnection

Return Value

Doctrine_Connection

protected boolean _hasDqlQueryPart(string $queryPartName)

Checks if there's at least one DQL part defined to the internal parts collection.

Parameters

string $queryPartName The name of the query part.

Return Value

boolean

protected _addDqlQueryPart(string $queryPartName, string $queryPart, boolean $append = false)

Adds a DQL part to the internal parts collection.

This method add the part specified to the array named by $queryPartName. Most part names support multiple parts addition.

Parameters

string $queryPartName The name of the query part.
string $queryPart The actual query part to add.
boolean $append Whether to append $queryPart to already existing parts under the same $queryPartName. Defaults to FALSE (previously added parts with the same name get overridden).

See also

$_dqlParts;
Doctrine_Query::getDqlPart

protected Doctrine_Query _processDqlQueryPart(string $queryPartName, array $queryParts)

_processDqlQueryPart parses given query part

Parameters

string $queryPartName the name of the query part
array $queryParts an array containing the query part data

Return Value

Doctrine_Query this object

protected Doctrine_Query_Part _getParser($name)

_getParser parser lazy-loader

Parameters

$name

Return Value

Doctrine_Query_Part

Exceptions

Doctrine_Query_Exception if unknown parser name given

getSqlQuery(array $params = array(), bool $limitSubquery = true)

builds the sql query from the given parameters and applies things such as column aggregation inheritance and limit subqueries if needed

Parameters

array $params
bool $limitSubquery Whether or not to try and apply the limit subquery algorithm

Doctrine_Query_Abstract parseDqlQuery(string $query, boolean $clear = true)

DQL PARSER parses a DQL query first splits the query in parts and then uses individual parsers for each part

Parameters

string $query query to be parsed
boolean $clear whether or not to clear the aliases

Return Value

Doctrine_Query_Abstract this object

Exceptions

Doctrine_Query_Exception if some generic parsing error occurs

string __toString()

toString magic call this method is automatically called when Doctrine_Query object is trying to be used as a string So, it it converted into its DQL correspondant

Return Value

string DQL string

boolean getDisableLimitSubquery()

Gets the disableLimitSubquery property.

Return Value

boolean

setDisableLimitSubquery(boolean $disableLimitSubquery)

Allows you to set the disableLimitSubquery property -- setting this to true will restrict the query object from using the limit sub query method of tranversing many relationships.

Parameters

boolean $disableLimitSubquery

static Doctrine_Query create(Doctrine_Connection $conn = null, string $class = null)

create returns a new Doctrine_Query object

Parameters

Doctrine_Connection $conn optional connection parameter
string $class Query class to instantiate

Return Value

Doctrine_Query

reset()

Resets the query to the state just after it has been instantiated.

Doctrine_Hydrate createSubquery()

createSubquery creates a subquery

Return Value

Doctrine_Hydrate

Doctrine_Query addPendingJoinCondition(string $componentAlias, string $joinCondition)

addPendingJoinCondition

Parameters

string $componentAlias component alias
string $joinCondition dql join condition

Return Value

Doctrine_Query this object

array fetchArray(string $params = array())

fetchArray Convenience method to execute using array fetching as hydration mode.

Parameters

string $params

Return Value

array

mixed fetchOne(string $params = array(), int $hydrationMode = null)

fetchOne Convenience method to execute the query and return the first item of the collection.

Parameters

string $params Query parameters
int $hydrationMode Hydration mode: see Doctrine_Core::HYDRATE_* constants

Return Value

mixed Array or Doctrine_Collection, depending on hydration mode. False if no result.

Doctrine_Query|bool isSubquery(boolean $bool = null)

isSubquery if $bool parameter is set this method sets the value of Doctrine_Query::$isSubquery. If this value is set to true the query object will not load the primary key fields of the selected components.

If null is given as the first parameter this method retrieves the current value of Doctrine_Query::$isSubquery.

Parameters

boolean $bool whether or not this query acts as a subquery

Return Value

Doctrine_Query|bool

string getSqlAggregateAlias(string $dqlAlias)

getSqlAggregateAlias

Parameters

string $dqlAlias the dql alias of an aggregate value

Return Value

string

boolean hasSqlAggregateAlias(string $dqlAlias)

Check if a dql alias has a sql aggregate alias

Parameters

string $dqlAlias

Return Value

boolean

adjustProcessedParam($index)

Adjust the processed param index for "foo.bar IN ?" support

Parameters

$index

array getDqlPart(string $queryPart)

Retrieves a specific DQL query part.

Parameters

string $queryPart the name of the query part; can be: array from, containing strings; array select, containg string; boolean forUpdate; array set; array join; array where; array groupby; array having; array orderby, containing strings such as 'id ASC'; array limit, containing numerics; array offset, containing numerics;

Return Value

array

See also

Doctrine_Query_Abstract::$_dqlParts var_dump($q->getDqlPart('where')); // array(2) { [0] => string(8) 'name = ?' [1] => string(8) 'date > ?' }

boolean contains(string $dql)

contains

Method to check if a arbitrary piece of dql exists

Parameters

string $dql Arbitrary piece of dql to check for

Return Value

boolean

string processPendingFields(string $componentAlias)

processPendingFields the fields in SELECT clause cannot be parsed until the components in FROM clause are parsed, hence this method is called everytime a specific component is being parsed. For instance, the wildcard '*' is expanded in the list of columns.

Parameters

string $componentAlias the alias of the component

Return Value

string SQL code

Exceptions

Doctrine_Query_Exception if unknown component alias has been given

string parseSelectField(string $field)

Parses a nested field $q->parseSelectField('u.Phonenumber.value');

Parameters

string $field

Return Value

string SQL fragment

Exceptions

Doctrine_Query_Exception if unknown component alias has been given

string getExpressionOwner(string $expr)

getExpressionOwner returns the component alias for owner of given expression

Parameters

string $expr expression from which to get to owner from

Return Value

string the component alias

parseSelect(string $dql)

parseSelect parses the query select part and adds selected fields to pendingFields array

Parameters

string $dql

string parseClause($clause)

parseClause parses given DQL clause

this method handles five tasks:

  1. Converts all DQL functions to their native SQL equivalents
  2. Converts all component references to their table alias equivalents
  3. Converts all field names to actual column names
  4. Quotes all identifiers
  5. Parses nested clauses and subqueries recursively

Parameters

$clause

Return Value

string SQL string

parseIdentifierReference($expr)

Parameters

$expr

parseFunctionExpression($expr, $parseCallback = null)

Parameters

$expr
$parseCallback

parseSubquery($subquery)

Parameters

$subquery

void processPendingSubqueries()

processPendingSubqueries processes pending subqueries

subqueries can only be processed when the query is fully constructed since some subqueries may be correlated

Return Value

void

void processPendingAggregates()

processPendingAggregates processes pending aggregate values for given component alias

Return Value

void

protected string _buildSqlQueryBase()

_buildSqlQueryBase returns the base of the generated sql query On mysql driver special strategy has to be used for DELETE statements (where is this special strategy??)

Return Value

string the base of the generated sql query

protected string _buildSqlFromPart($ignorePending = false)

_buildSqlFromPart builds the from part of the query and returns it

Parameters

$ignorePending

Return Value

string the query sql from part

protected Processed _processPendingJoinConditions(string $alias)

Processes the pending join conditions, used for dynamically add conditions to root component/joined components without interfering in the main dql handling.

Parameters

string $alias Component Alias

Return Value

Processed pending conditions

string buildSqlQuery(bool $limitSubquery = true)

Build the SQL query from the DQL

Parameters

bool $limitSubquery Whether or not to try and apply the limit subquery algorithm

Return Value

string $sql The generated SQL string

string getLimitSubquery()

getLimitSubquery this is method is used by the record limit algorithm

when fetching one-to-many, many-to-many associated data with LIMIT clause an additional subquery is needed for limiting the number of returned records instead of limiting the number of sql result set rows

Return Value

string the limit subquery

load(string $path, boolean $loadFields = true)

Parameters

string $path component alias
boolean $loadFields

protected buildSimpleRelationSql(Doctrine_Relation $relation, $foreignAlias, $localAlias, $overrideJoin, $join)

Parameters

Doctrine_Relation $relation
$foreignAlias
$localAlias
$overrideJoin
$join

protected buildIndexBy($componentAlias, $mapWith = null)

Parameters

$componentAlias
$mapWith

protected buildAssociativeRelationSql(Doctrine_Relation $relation, $assocAlias, $foreignAlias, $localAlias)

Parameters

Doctrine_Relation $relation
$assocAlias
$foreignAlias
$localAlias

Doctrine_Table loadRoot(string $name, string $componentAlias)

loadRoot

Parameters

string $name
string $componentAlias

Return Value

Doctrine_Table

string buildInheritanceJoinSql(string $name, string $componentAlias)

Parameters

string $name component class name
string $componentAlias alias of the component in the dql

Return Value

string query part

string getCountSqlQuery()

Get count sql query for this Doctrine_Query instance.

This method is used in Doctrine_Query::count() for returning an integer for the number of records which will be returned when executed.

Return Value

string $q

integer count(array $params = array())

Fetches the count of the query.

This method executes the main query without all the selected fields, ORDER BY part, LIMIT part and OFFSET part.

Example: Main query: SELECT u.*, p.phonenumber FROM User u LEFT JOIN u.Phonenumber p WHERE p.phonenumber = '123 123' LIMIT 10

The modified DQL query: SELECT COUNT(DISTINCT u.id) FROM User u LEFT JOIN u.Phonenumber p WHERE p.phonenumber = '123 123'

Parameters

array $params an array of prepared statement parameters

Return Value

integer the count of this query

mixed query(string $query, array $params = array(), int $hydrationMode = null)

Queries the database with DQL (Doctrine Query Language).

This methods parses a Dql query and builds the query parts.

Parameters

string $query Dql query
array $params prepared statement parameters
int $hydrationMode Doctrine_Core::HYDRATE_ARRAY or Doctrine_Core::HYDRATE_RECORD

Return Value

mixed

See also

Doctrine_Core::FETCH_* constants

Doctrine_Query copy(Doctrine_Query $query = null)

Copies a Doctrine_Query object.

Parameters

Doctrine_Query $query

Return Value

Doctrine_Query Copy of the Doctrine_Query instance.

void __clone()

Magic method called after cloning process.

Return Value

void

protected _killReference(string $key)

Kill the reference for the passed class property.

This method simply copies the value to a temporary variable and then unsets the reference and re-assigns the old value but not by reference

Parameters

string $key