8 code examples of PHP Oro\Component\DoctrineUtils\ORM\QueryUtil extracted from open source projects
/**
* Protects a query by ACL.
*
* @param Query|QueryBuilder $query The query to be protected
* @param string $permission The permission to apply
* @param array $options Additional options for access rules.
* The following options are implemented out-of-the-box:
* * "checkRootEntity" determined whether the root entity should be
* protected; default value is TRUE
* * "checkRelations" determined whether entities associated with
* the root entity should be protected; default value is TRUE
* To find all possible options see classes that implement
* Oro\Bundle\SecurityBundle\AccessRule\AccessRuleInterface
*/
public function apply($query, string $permission = 'VIEW', array $options = []): Query
{
$context = $this->contextFactory->createContext($permission);
foreach ($options as $optionName => $value) {
$context->setOption($optionName, $value);
}
if ($query instanceof QueryBuilder) {
$query = $query->getQuery();
}
QueryUtil::addTreeWalker($query, AccessRuleWalker::class);
$query->setHint(AccessRuleWalker::CONTEXT, $context);
return $query;
}
/**
* @throws QueryException
*/
public function processParameterMappings(Query $query): array
{
$paramMappings = QueryUtil::parseQuery($query)->getParameterMappings();
$paramCount = count($query->getParameters());
$mappingCount = count($paramMappings);
if ($paramCount > $mappingCount) {
throw QueryException::tooManyParameters($mappingCount, $paramCount);
}
if ($paramCount < $mappingCount) {
throw QueryException::tooFewParameters($mappingCount, $paramCount);
}
return QueryUtil::processParameterMappings($query, $paramMappings);
}
/**
* @inheritdoc
*/
public function initializeIdentityQuery(Query $query)
{
$identifierHydrationMode = 'IdentifierHydrator';
$query
->getEntityManager()
->getConfiguration()
->addCustomHydrationMode($identifierHydrationMode, IdentifierHydrator::class);
$query->setHydrationMode($identifierHydrationMode);
QueryUtil::addTreeWalker($query, ReduceOrderByWalker::class);
QueryUtil::addTreeWalker($query, SelectIdentifierWalker::class);
}
/**
* @inheritdoc
*/
public function initializeDataQuery(Query $query)
{
QueryUtil::addTreeWalker($query, LimitIdentifierWalker::class);
// limit and offset implemented with ids
$query->setFirstResult(null);
$query->setMaxResults(null);
}
/**
* @inheritdoc
*/
public function initializeIdentityQuery(Query $query)
{
$identifierHydrationMode = 'IdentifierHydrator';
$query
->getEntityManager()
->getConfiguration()
->addCustomHydrationMode($identifierHydrationMode, IdentifierHydrator::class);
$query->setHydrationMode($identifierHydrationMode);
QueryUtil::addTreeWalker($query, SelectIdentifierWalker::class);
}
private function executeOrmCountQueryUsingCountWalker(Query $query): int
{
$countQuery = QueryUtil::cloneQuery($query);
if (null !== $this->shouldUseDistinct) {
$countQuery->setHint(CountWalker::HINT_DISTINCT, $this->shouldUseDistinct);
} elseif (!$countQuery->hasHint(CountWalker::HINT_DISTINCT)) {
$countQuery->setHint(CountWalker::HINT_DISTINCT, $countQuery->contains('DISTINCT'));
}
QueryUtil::addTreeWalker($countQuery, CountWalker::class);
$this->unbindUnusedQueryParams($countQuery, QueryUtil::parseQuery($countQuery));
$countQuery->setFirstResult(null)->setMaxResults(null);
// the result-set mapping is not relevant and should be rebuilt because the query is be changed
QueryUtil::resetResultSetMapping($countQuery);
try {
return array_sum(array_map('current', $countQuery->getScalarResult()));
} catch (NoResultException $e) {
return 0;
}
}
private function executeOrmCountQueryUsingCountWalker(Query $query): int
{
$countQuery = QueryUtil::cloneQuery($query);
if (null !== $this->shouldUseDistinct) {
$countQuery->setHint(CountWalker::HINT_DISTINCT, $this->shouldUseDistinct);
} elseif (!$countQuery->hasHint(CountWalker::HINT_DISTINCT)) {
$countQuery->setHint(CountWalker::HINT_DISTINCT, $countQuery->contains('DISTINCT'));
}
QueryUtil::addTreeWalker($countQuery, CountWalker::class);
$this->unbindUnusedQueryParams($countQuery, QueryUtil::parseQuery($countQuery));
$countQuery->setFirstResult(null)->setMaxResults(null);
// the result-set mapping is not relevant and should be rebuilt because the query is be changed
QueryUtil::resetResultSetMapping($countQuery);
try {
return array_sum(array_map('current', $countQuery->getScalarResult()));
} catch (NoResultException $e) {
return 0;
}
}
/**
* Makes full clone of the given query, including its parameters and hints
*/
protected function cloneQuery(Query $query): Query
{
return QueryUtil::cloneQuery($query);
}