NativeQuery
extends AbstractQuery
in package
Represents a native SQL query.
Table of Contents
Constants
- HYDRATE_ARRAY = 2
- Hydrates an array graph.
- HYDRATE_OBJECT = 1
- Hydrates an object graph. This is the default behavior.
- HYDRATE_SCALAR = 3
- Hydrates a flat, rectangular result set with scalar values.
- HYDRATE_SCALAR_COLUMN = 6
- Hydrates scalar column value.
- HYDRATE_SIMPLEOBJECT = 5
- Very simple object hydrator (optimized for performance).
- HYDRATE_SINGLE_SCALAR = 4
- Hydrates a single scalar value.
Properties
- $_em : EntityManagerInterface
- The entity manager used by this query object.
- $_expireResultCache : bool
- Whether or not expire the result cache.
- $_hints : mixed
- The map of query hints.
- $_hydrationCacheProfile : QueryCacheProfile|null
- $_hydrationMode : string|int
- The hydration mode.
- $_queryCacheProfile : QueryCacheProfile|null
- $_resultSetMapping : ResultSetMapping|null
- The user-specified ResultSetMapping to use.
- $cacheable : bool
- Whether to use second level cache, if available.
- $cacheLogger : CacheLogger|null
- $cacheMode : int|null
- Second level query cache mode.
- $cacheRegion : string|null
- Second level cache region name.
- $hasCache : bool
- $lifetime : int
- $parameters : ArrayCollection|array<string|int, Parameter>
- The parameter map of this query.
- $sql : string
Methods
- __clone() : void
- Cleanup Query resource when clone is called.
- __construct() : mixed
- Initializes a new instance of a class derived from <tt>AbstractQuery</tt>.
- disableResultCache() : $this
- Disables caching of the results of this query.
- enableResultCache() : $this
- Enables caching of the results of this query, for given or default amount of seconds and optionally specifies which ID to use for the cache entry.
- execute() : mixed
- Executes the query.
- expireResultCache() : $this
- Defines if the result cache is active or not.
- free() : void
- Frees the resources used by the query object.
- getArrayResult() : array<string|int, mixed>
- Gets the array of results for the query.
- getCacheMode() : int|null
- getCacheRegion() : string|null
- Obtain the name of the second level query cache region in which query results will be stored
- getEntityManager() : EntityManagerInterface
- Retrieves the associated EntityManager of this Query instance.
- getExpireResultCache() : bool
- Retrieves if the resultset cache is active or not.
- getHint() : mixed
- Gets the value of a query hint. If the hint name is not recognized, FALSE is returned.
- getHints() : array<string, mixed>
- Return the key value map of query hints that are currently set.
- getHydrationCacheProfile() : QueryCacheProfile|null
- getHydrationMode() : string|int
- Gets the hydration mode currently used by the query.
- getLifetime() : int
- getOneOrNullResult() : mixed
- Get exactly one result or null.
- getParameter() : Parameter|null
- Gets a query parameter.
- getParameters() : ArrayCollection
- Get all defined parameters.
- getQueryCacheProfile() : QueryCacheProfile|null
- getResult() : mixed
- Gets the list of results for the query.
- getResultCacheDriver() : Cache
- Returns the cache driver used for caching result sets.
- getResultCacheId() : string|null
- Get the result cache id to use to store the result set cache entry if set.
- getResultCacheLifetime() : int
- Retrieves the lifetime of resultset cache.
- getScalarResult() : array<string|int, mixed>
- Gets the scalar results for the query.
- getSingleColumnResult() : array<string|int, mixed>
- Gets one-dimensional array of results for the query.
- getSingleResult() : mixed
- Gets the single result of the query.
- getSingleScalarResult() : mixed
- Gets the single scalar result of the query.
- getSQL() : array<int, string>|string
- Gets the SQL query.
- hasHint() : bool
- Check if the query has a hint
- isCacheable() : bool
- iterate() : IterableResult
- Executes the query and returns an IterableResult that can be used to incrementally iterate over the result.
- processParameterValue() : mixed
- Processes an individual parameter value.
- setCacheable() : $this
- Enable/disable second level query (result) caching for this query.
- setCacheMode() : $this
- setCacheRegion() : $this
- setFetchMode() : $this
- Change the default fetch mode of an association for this query.
- setHint() : $this
- Sets a query hint. If the hint name is not recognized, it is silently ignored.
- setHydrationCacheProfile() : $this
- Set a cache profile for hydration caching.
- setHydrationMode() : $this
- Defines the processing mode to be used during hydration / result set transformation.
- setLifetime() : $this
- Sets the life-time for this query into second level cache.
- setParameter() : $this
- Sets a query parameter.
- setParameters() : $this
- Sets a collection of query parameters.
- setResultCache() : $this
- Defines a cache driver to be used for caching result sets and implicitly enables caching.
- setResultCacheDriver() : $this
- Defines a cache driver to be used for caching result sets and implicitly enables caching.
- setResultCacheId() : $this
- Set the result cache id to use to store the result set cache entry.
- setResultCacheLifetime() : $this
- Defines how long the result cache will be active before expire.
- setResultCacheProfile() : $this
- Set a cache profile for the result cache.
- setResultSetMapping() : $this
- Sets the ResultSetMapping that should be used for hydration.
- setSQL() : $this
- Sets the SQL of the query.
- toIterable() : iterable<string|int, mixed>
- Executes the query and returns an iterable that can be used to incrementally iterate over the result.
- useResultCache() : $this
- Set whether or not to cache the results of this query and if so, for how long and which ID to use for the cache entry.
- _doExecute() : Result|int
- Executes the query and returns a the resulting Statement object.
- getHash() : string
- Generates a string of currently query to use for the cache second level cache.
- getHydrationCacheId() : array<string|int, string>
- Get the result cache id to use to store the result set cache entry.
- getResultSetMapping() : ResultSetMapping|null
- Gets the ResultSetMapping used for hydration.
- isCacheEnabled() : bool
- executeIgnoreQueryCache() : mixed
- Execute query ignoring second level cache.
- executeUsingQueryCache() : mixed
- Load from second level cache or executes the query and put into cache.
- getHydrationCache() : CacheItemPoolInterface
- getTimestampKey() : TimestampCacheKey|null
- isCountable() : bool
- potentiallyProcessIterable() : mixed
- If no mapping is detected, trying to resolve the value as a Traversable
- processArrayParameterValue() : array<string|int, mixed>
- Process a parameter value which was previously identified as an array
- translateNamespaces() : void
- Allows to translate entity namespaces to full qualified names.
Constants
HYDRATE_ARRAY
Hydrates an array graph.
public
mixed
HYDRATE_ARRAY
= 2
HYDRATE_OBJECT
Hydrates an object graph. This is the default behavior.
public
mixed
HYDRATE_OBJECT
= 1
HYDRATE_SCALAR
Hydrates a flat, rectangular result set with scalar values.
public
mixed
HYDRATE_SCALAR
= 3
HYDRATE_SCALAR_COLUMN
Hydrates scalar column value.
public
mixed
HYDRATE_SCALAR_COLUMN
= 6
HYDRATE_SIMPLEOBJECT
Very simple object hydrator (optimized for performance).
public
mixed
HYDRATE_SIMPLEOBJECT
= 5
HYDRATE_SINGLE_SCALAR
Hydrates a single scalar value.
public
mixed
HYDRATE_SINGLE_SCALAR
= 4
Properties
$_em
The entity manager used by this query object.
protected
EntityManagerInterface
$_em
$_expireResultCache
Whether or not expire the result cache.
protected
bool
$_expireResultCache
= false
$_hints
The map of query hints.
protected
mixed
$_hints
= []
Tags
$_hydrationCacheProfile
protected
QueryCacheProfile|null
$_hydrationCacheProfile
$_hydrationMode
The hydration mode.
protected
string|int
$_hydrationMode
= self::HYDRATE_OBJECT
Tags
$_queryCacheProfile
protected
QueryCacheProfile|null
$_queryCacheProfile
$_resultSetMapping
The user-specified ResultSetMapping to use.
protected
ResultSetMapping|null
$_resultSetMapping
$cacheable
Whether to use second level cache, if available.
protected
bool
$cacheable
= false
$cacheLogger
protected
CacheLogger|null
$cacheLogger
$cacheMode
Second level query cache mode.
protected
int|null
$cacheMode
Tags
$cacheRegion
Second level cache region name.
protected
string|null
$cacheRegion
$hasCache
protected
bool
$hasCache
= false
$lifetime
protected
int
$lifetime
= 0
$parameters
The parameter map of this query.
protected
ArrayCollection|array<string|int, Parameter>
$parameters
Tags
$sql
private
string
$sql
Methods
__clone()
Cleanup Query resource when clone is called.
public
__clone() : void
__construct()
Initializes a new instance of a class derived from <tt>AbstractQuery</tt>.
public
__construct(EntityManagerInterface $em) : mixed
Parameters
- $em : EntityManagerInterface
disableResultCache()
Disables caching of the results of this query.
public
disableResultCache() : $this
Return values
$thisenableResultCache()
Enables caching of the results of this query, for given or default amount of seconds and optionally specifies which ID to use for the cache entry.
public
enableResultCache([int|null $lifetime = null ][, string|null $resultCacheId = null ]) : $this
Parameters
- $lifetime : int|null = null
-
How long the cache entry is valid, in seconds.
- $resultCacheId : string|null = null
-
ID to use for the cache entry.
Return values
$thisexecute()
Executes the query.
public
execute([ArrayCollection|array<string|int, mixed>|null $parameters = null ][, string|int|null $hydrationMode = null ]) : mixed
Parameters
- $parameters : ArrayCollection|array<string|int, mixed>|null = null
-
Query parameters.
- $hydrationMode : string|int|null = null
-
Processing mode to be used during the hydration process.
Tags
expireResultCache()
Defines if the result cache is active or not.
public
expireResultCache([bool $expire = true ]) : $this
Parameters
- $expire : bool = true
-
Whether or not to force resultset cache expiration.
Return values
$thisfree()
Frees the resources used by the query object.
public
free() : void
Resets Parameters, Parameter Types and Query Hints.
getArrayResult()
Gets the array of results for the query.
public
getArrayResult() : array<string|int, mixed>
Alias for execute(null, HYDRATE_ARRAY).
Return values
array<string|int, mixed>getCacheMode()
public
getCacheMode() : int|null
Tags
Return values
int|nullgetCacheRegion()
Obtain the name of the second level query cache region in which query results will be stored
public
getCacheRegion() : string|null
Return values
string|null —The cache region name; NULL indicates the default region.
getEntityManager()
Retrieves the associated EntityManager of this Query instance.
public
getEntityManager() : EntityManagerInterface
Return values
EntityManagerInterfacegetExpireResultCache()
Retrieves if the resultset cache is active or not.
public
getExpireResultCache() : bool
Return values
boolgetHint()
Gets the value of a query hint. If the hint name is not recognized, FALSE is returned.
public
getHint(string $name) : mixed
Parameters
- $name : string
-
The name of the hint.
Return values
mixed —The value of the hint or FALSE, if the hint name is not recognized.
getHints()
Return the key value map of query hints that are currently set.
public
getHints() : array<string, mixed>
Return values
array<string, mixed>getHydrationCacheProfile()
public
getHydrationCacheProfile() : QueryCacheProfile|null
Return values
QueryCacheProfile|nullgetHydrationMode()
Gets the hydration mode currently used by the query.
public
getHydrationMode() : string|int
Tags
Return values
string|intgetLifetime()
public
getLifetime() : int
Return values
intgetOneOrNullResult()
Get exactly one result or null.
public
getOneOrNullResult([string|int|null $hydrationMode = null ]) : mixed
Parameters
- $hydrationMode : string|int|null = null
Tags
getParameter()
Gets a query parameter.
public
getParameter(int|string $key) : Parameter|null
Parameters
- $key : int|string
-
The key (index or name) of the bound parameter.
Return values
Parameter|null —The value of the bound parameter, or NULL if not available.
getParameters()
Get all defined parameters.
public
getParameters() : ArrayCollection
Tags
Return values
ArrayCollection —The defined query parameters.
getQueryCacheProfile()
public
getQueryCacheProfile() : QueryCacheProfile|null
Return values
QueryCacheProfile|nullgetResult()
Gets the list of results for the query.
public
getResult([string|int $hydrationMode = self::HYDRATE_OBJECT ]) : mixed
Alias for execute(null, $hydrationMode = HYDRATE_OBJECT).
Parameters
- $hydrationMode : string|int = self::HYDRATE_OBJECT
Tags
getResultCacheDriver()
Returns the cache driver used for caching result sets.
public
getResultCacheDriver() : Cache
Tags
Return values
Cache —Cache driver
getResultCacheId()
Get the result cache id to use to store the result set cache entry if set.
public
getResultCacheId() : string|null
Tags
Return values
string|nullgetResultCacheLifetime()
Retrieves the lifetime of resultset cache.
public
getResultCacheLifetime() : int
Tags
Return values
intgetScalarResult()
Gets the scalar results for the query.
public
getScalarResult() : array<string|int, mixed>
Alias for execute(null, HYDRATE_SCALAR).
Return values
array<string|int, mixed>getSingleColumnResult()
Gets one-dimensional array of results for the query.
public
getSingleColumnResult() : array<string|int, mixed>
Alias for execute(null, HYDRATE_SCALAR_COLUMN).
Return values
array<string|int, mixed>getSingleResult()
Gets the single result of the query.
public
getSingleResult([string|int|null $hydrationMode = null ]) : mixed
Enforces the presence as well as the uniqueness of the result.
If the result is not unique, a NonUniqueResultException is thrown. If there is no result, a NoResultException is thrown.
Parameters
- $hydrationMode : string|int|null = null
Tags
getSingleScalarResult()
Gets the single scalar result of the query.
public
getSingleScalarResult() : mixed
Alias for getSingleResult(HYDRATE_SINGLE_SCALAR).
Tags
Return values
mixed —The scalar result.
getSQL()
Gets the SQL query.
public
getSQL() : array<int, string>|string
Return values
array<int, string>|string —SQL query
hasHint()
Check if the query has a hint
public
hasHint(string $name) : bool
Parameters
- $name : string
-
The name of the hint
Return values
bool —False if the query does not have any hint
isCacheable()
public
isCacheable() : bool
Return values
bool —TRUE if the query results are enabled for second level cache, FALSE otherwise.
iterate()
Executes the query and returns an IterableResult that can be used to incrementally iterate over the result.
public
iterate([ArrayCollection|array<string|int, mixed>|null $parameters = null ][, string|int|null $hydrationMode = null ]) : IterableResult
Parameters
- $parameters : ArrayCollection|array<string|int, mixed>|null = null
-
The query parameters.
- $hydrationMode : string|int|null = null
-
The hydration mode to use.
Tags
Return values
IterableResultprocessParameterValue()
Processes an individual parameter value.
public
processParameterValue(mixed $value) : mixed
Parameters
- $value : mixed
Tags
setCacheable()
Enable/disable second level query (result) caching for this query.
public
setCacheable(bool $cacheable) : $this
Parameters
- $cacheable : bool
Return values
$thissetCacheMode()
public
setCacheMode(int $cacheMode) : $this
Parameters
- $cacheMode : int
Tags
Return values
$thissetCacheRegion()
public
setCacheRegion(string $cacheRegion) : $this
Parameters
- $cacheRegion : string
Return values
$thissetFetchMode()
Change the default fetch mode of an association for this query.
public
setFetchMode(class-string $class, string $assocName, int $fetchMode) : $this
Parameters
- $class : class-string
- $assocName : string
- $fetchMode : int
Tags
Return values
$thissetHint()
Sets a query hint. If the hint name is not recognized, it is silently ignored.
public
setHint(string $name, mixed $value) : $this
Parameters
- $name : string
-
The name of the hint.
- $value : mixed
-
The value of the hint.
Return values
$thissetHydrationCacheProfile()
Set a cache profile for hydration caching.
public
setHydrationCacheProfile([QueryCacheProfile|null $profile = null ]) : $this
If no result cache driver is set in the QueryCacheProfile, the default result cache driver is used from the configuration.
Important: Hydration caching does NOT register entities in the UnitOfWork when retrieved from the cache. Never use result cached entities for requests that also flush the EntityManager. If you want some form of caching with UnitOfWork registration you should use AbstractQuery::setResultCacheProfile().
Parameters
- $profile : QueryCacheProfile|null = null
Tags
Return values
$thissetHydrationMode()
Defines the processing mode to be used during hydration / result set transformation.
public
setHydrationMode(string|int $hydrationMode) : $this
Parameters
- $hydrationMode : string|int
-
Doctrine processing mode to be used during hydration process. One of the Query::HYDRATE_* constants.
Tags
Return values
$thissetLifetime()
Sets the life-time for this query into second level cache.
public
setLifetime(int $lifetime) : $this
Parameters
- $lifetime : int
Return values
$thissetParameter()
Sets a query parameter.
public
setParameter(string|int $key, mixed $value[, string|int|null $type = null ]) : $this
Parameters
- $key : string|int
-
The parameter position or name.
- $value : mixed
-
The parameter value.
- $type : string|int|null = null
-
The parameter type. If specified, the given value will be run through the type conversion of this type. This is usually not needed for strings and numeric types.
Return values
$thissetParameters()
Sets a collection of query parameters.
public
setParameters(ArrayCollection|array<string|int, mixed> $parameters) : $this
Parameters
- $parameters : ArrayCollection|array<string|int, mixed>
Tags
Return values
$thissetResultCache()
Defines a cache driver to be used for caching result sets and implicitly enables caching.
public
setResultCache([CacheItemPoolInterface|null $resultCache = null ]) : $this
Parameters
- $resultCache : CacheItemPoolInterface|null = null
Return values
$thissetResultCacheDriver()
Defines a cache driver to be used for caching result sets and implicitly enables caching.
public
setResultCacheDriver([Cache|null $resultCacheDriver = null ]) : $this
Parameters
- $resultCacheDriver : Cache|null = null
-
Cache driver
Tags
Return values
$thissetResultCacheId()
Set the result cache id to use to store the result set cache entry.
public
setResultCacheId(string|null $id) : $this
If this is not explicitly set by the developer then a hash is automatically generated for you.
Parameters
- $id : string|null
Return values
$thissetResultCacheLifetime()
Defines how long the result cache will be active before expire.
public
setResultCacheLifetime(int|null $lifetime) : $this
Parameters
- $lifetime : int|null
-
How long the cache entry is valid, in seconds.
Return values
$thissetResultCacheProfile()
Set a cache profile for the result cache.
public
setResultCacheProfile([QueryCacheProfile|null $profile = null ]) : $this
If no result cache driver is set in the QueryCacheProfile, the default result cache driver is used from the configuration.
Parameters
- $profile : QueryCacheProfile|null = null
Return values
$thissetResultSetMapping()
Sets the ResultSetMapping that should be used for hydration.
public
setResultSetMapping(ResultSetMapping $rsm) : $this
Parameters
- $rsm : ResultSetMapping
Return values
$thissetSQL()
Sets the SQL of the query.
public
setSQL(string $sql) : $this
Parameters
- $sql : string
Return values
$thistoIterable()
Executes the query and returns an iterable that can be used to incrementally iterate over the result.
public
toIterable([ArrayCollection|array<string|int, mixed> $parameters = [] ][, string|int|null $hydrationMode = null ]) : iterable<string|int, mixed>
Parameters
- $parameters : ArrayCollection|array<string|int, mixed> = []
-
The query parameters.
- $hydrationMode : string|int|null = null
-
The hydration mode to use.
Tags
Return values
iterable<string|int, mixed>useResultCache()
Set whether or not to cache the results of this query and if so, for how long and which ID to use for the cache entry.
public
useResultCache(bool $useCache[, int $lifetime = null ][, string $resultCacheId = null ]) : $this
Parameters
- $useCache : bool
-
Whether or not to cache the results of this query.
- $lifetime : int = null
-
How long the cache entry is valid, in seconds.
- $resultCacheId : string = null
-
ID to use for the cache entry.
Tags
Return values
$this_doExecute()
Executes the query and returns a the resulting Statement object.
protected
_doExecute() : Result|int
Return values
Result|int —The executed database statement that holds the results, or an integer indicating how many rows were affected.
getHash()
Generates a string of currently query to use for the cache second level cache.
protected
getHash() : string
Return values
stringgetHydrationCacheId()
Get the result cache id to use to store the result set cache entry.
protected
getHydrationCacheId() : array<string|int, string>
Will return the configured id if it exists otherwise a hash will be automatically generated for you.
Tags
Return values
array<string|int, string> —($key, $hash)
getResultSetMapping()
Gets the ResultSetMapping used for hydration.
protected
getResultSetMapping() : ResultSetMapping|null
Return values
ResultSetMapping|nullisCacheEnabled()
protected
isCacheEnabled() : bool
Return values
bool —TRUE if the query cache and second level cache are enabled, FALSE otherwise.
executeIgnoreQueryCache()
Execute query ignoring second level cache.
private
executeIgnoreQueryCache([ArrayCollection|array<string|int, mixed>|null $parameters = null ][, string|int|null $hydrationMode = null ]) : mixed
Parameters
- $parameters : ArrayCollection|array<string|int, mixed>|null = null
- $hydrationMode : string|int|null = null
Tags
executeUsingQueryCache()
Load from second level cache or executes the query and put into cache.
private
executeUsingQueryCache([ArrayCollection|array<string|int, mixed>|null $parameters = null ][, string|int|null $hydrationMode = null ]) : mixed
Parameters
- $parameters : ArrayCollection|array<string|int, mixed>|null = null
- $hydrationMode : string|int|null = null
Tags
getHydrationCache()
private
getHydrationCache() : CacheItemPoolInterface
Return values
CacheItemPoolInterfacegetTimestampKey()
private
getTimestampKey() : TimestampCacheKey|null
Return values
TimestampCacheKey|nullisCountable()
private
isCountable(iterable<string|int, mixed> $subject) : bool
Parameters
- $subject : iterable<string|int, mixed>
Return values
boolpotentiallyProcessIterable()
If no mapping is detected, trying to resolve the value as a Traversable
private
potentiallyProcessIterable(mixed $value) : mixed
Parameters
- $value : mixed
processArrayParameterValue()
Process a parameter value which was previously identified as an array
private
processArrayParameterValue(array<string|int, mixed> $value) : array<string|int, mixed>
Parameters
- $value : array<string|int, mixed>
Return values
array<string|int, mixed>translateNamespaces()
Allows to translate entity namespaces to full qualified names.
private
translateNamespaces(ResultSetMapping $rsm) : void
Parameters
- $rsm : ResultSetMapping