示例#1
0
 /**
  * @param array $associationMapping
  * @param array $entityIds
  * @param array $config
  *
  * @return array [['entityId' => mixed, 'relatedEntityId' => mixed], ...]
  */
 public function getRelatedItemsIds($associationMapping, $entityIds, $config)
 {
     $limit = isset($config[ConfigUtil::MAX_RESULTS]) ? $config[ConfigUtil::MAX_RESULTS] : -1;
     if ($limit > 0 && count($entityIds) > 1) {
         $selectStmt = null;
         $subQueries = [];
         foreach ($entityIds as $id) {
             $subQuery = $this->getRelatedItemsIdsQuery($associationMapping, [$id], $config);
             $subQuery->setMaxResults($limit);
             // We should wrap all subqueries with brackets for PostgreSQL queries with UNION and LIMIT
             $subQueries[] = '(' . QueryUtils::getExecutableSql($subQuery) . ')';
             if (null === $selectStmt) {
                 $mapping = QueryUtils::parseQuery($subQuery)->getResultSetMapping();
                 $selectStmt = sprintf('entity.%s AS entityId, entity.%s AS relatedEntityId', QueryUtils::getColumnNameByAlias($mapping, 'entityId'), QueryUtils::getColumnNameByAlias($mapping, 'relatedEntityId'));
             }
         }
         $rsm = new ResultSetMapping();
         $rsm->addScalarResult('entityId', 'entityId')->addScalarResult('relatedEntityId', 'relatedEntityId');
         $qb = new SqlQueryBuilder($this->doctrineHelper->getEntityManager($associationMapping['targetEntity']), $rsm);
         $qb->select($selectStmt)->from('(' . implode(' UNION ALL ', $subQueries) . ')', 'entity');
         $rows = $qb->getQuery()->getScalarResult();
     } else {
         $query = $this->getRelatedItemsIdsQuery($associationMapping, $entityIds, $config);
         if ($limit >= 0) {
             $query->setMaxResults($limit);
         }
         $rows = $query->getScalarResult();
     }
     return $rows;
 }
示例#2
0
 /**
  * Calculates total count of query records
  * Notes: this method do not make any modifications of the given query
  *
  * @param Query|SqlQuery $query
  *
  * @return integer
  */
 public function getCount($query)
 {
     if ($this->useWalker($query)) {
         if (!$query->contains('DISTINCT')) {
             $query->setHint(CountWalker::HINT_DISTINCT, false);
         }
         $paginator = new Paginator($query);
         $paginator->setUseOutputWalkers(false);
         $result = $paginator->count();
     } else {
         if ($query instanceof Query) {
             $parserResult = QueryUtils::parseQuery($query);
             $parameterMappings = $parserResult->getParameterMappings();
             list($params, $types) = QueryUtils::processParameterMappings($query, $parameterMappings);
             $statement = $query->getEntityManager()->getConnection()->executeQuery(sprintf('SELECT COUNT(*) FROM (%s) AS e', $query->getSQL()), $params, $types);
         } elseif ($query instanceof SqlQuery) {
             $countQuery = clone $query->getQueryBuilder();
             $statement = $countQuery->resetQueryParts()->select('COUNT(*)')->from('(' . $query->getSQL() . ')', 'e')->execute();
         } else {
             throw new \InvalidArgumentException(sprintf('Expected instance of Doctrine\\ORM\\Query' . ' or Oro\\Component\\DoctrineUtils\\ORM\\SqlQuery, "%s" given', is_object($query) ? get_class($query) : gettype($query)));
         }
         $result = $statement->fetchColumn();
     }
     return $result ? (int) $result : 0;
 }
示例#3
0
 /**
  * Gets prepared SQL and parameters from executed query
  * and stores them in DataGrid config object
  *
  * @param OrmResultAfter $event
  */
 public function onResultAfter(OrmResultAfter $event)
 {
     $config = $event->getDatagrid()->getConfig();
     $path = sprintf('%s[%s]', MetadataObject::OPTIONS_KEY, StoreSqlExtension::STORE_SQL);
     if ($config->offsetGetByPath($path, false)) {
         $config->offsetAddToArrayByPath(StoreSqlExtension::STORED_SQL_PATH, [StoreSqlExtension::SQL => QueryUtils::getExecutableSql($event->getQuery())]);
     }
 }
示例#4
0
 /**
  * Gets the root entity alias of the query.
  * This method rethrows QueryException as InvalidEntityException to avoid BC break
  *
  * @param QueryBuilder $qb             The query builder
  * @param bool         $throwException Whether to throw exception in case the query does not have a root alias
  *
  * @return string|null
  *
  * @throws Exception\InvalidEntityException
  */
 public static function getSingleRootAlias(QueryBuilder $qb, $throwException = true)
 {
     try {
         return BaseQueryUtils::getSingleRootAlias($qb, $throwException);
     } catch (QueryException $e) {
         throw new Exception\InvalidEntityException($e->getMessage());
     }
 }
示例#5
0
 /**
  * {@inheritdoc}
  */
 public function apply(Criteria $criteria, FilterValue $value = null)
 {
     $val = null !== $value ? $value->getValue() : $this->getDefaultValue();
     if (null !== $val) {
         $pageSize = $criteria->getMaxResults();
         if (null !== $pageSize) {
             $criteria->setFirstResult(QueryUtils::getPageOffset($val, $pageSize));
         }
     }
 }
示例#6
0
 /**
  * Adds criteria to the query.
  *
  * @param QueryBuilder $qb
  * @param Criteria     $criteria
  */
 public function applyCriteria(QueryBuilder $qb, Criteria $criteria)
 {
     $joins = $criteria->getJoins();
     if (!empty($joins)) {
         $rootAlias = QueryUtils::getSingleRootAlias($qb);
         foreach ($joins as $join) {
             $alias = $join->getAlias();
             $joinExpr = str_replace(Criteria::ROOT_ALIAS_PLACEHOLDER, $rootAlias, $join->getJoin());
             $condition = $join->getCondition();
             if ($condition) {
                 $condition = strtr($condition, [Criteria::ROOT_ALIAS_PLACEHOLDER => $rootAlias, Criteria::ENTITY_ALIAS_PLACEHOLDER => $alias]);
             }
             $method = strtolower($join->getJoinType()) . 'Join';
             $qb->{$method}($joinExpr, $alias, $join->getConditionType(), $condition, $join->getIndexBy());
         }
     }
     $qb->addCriteria($criteria);
 }
示例#7
0
 /**
  * Query builder to get target entities in a single query
  *
  * @param array $groupedTargets
  *
  * @return SqlQueryBuilder
  * @throws \Doctrine\ORM\Query\QueryException
  */
 protected function getAssociatedTargetEntitiesQueryBuilder(array $groupedTargets)
 {
     /** @var EntityManager $objectManager */
     $objectManager = $this->objectManager;
     $selectStmt = null;
     $subQueries = [];
     foreach ($groupedTargets as $entityClass => $ids) {
         $nameExpr = $this->getNameDQL($entityClass, 'e');
         /** @var QueryBuilder $subQb */
         $subQb = $objectManager->getRepository($entityClass)->createQueryBuilder('e')->select(sprintf('e.id AS id, \'%s\' AS entityClass, ' . ($nameExpr ?: '\'\'') . ' AS entityTitle', str_replace('\'', '\'\'', $entityClass)));
         $subQb->where($subQb->expr()->in('e.id', $ids));
         $subQuery = $subQb->getQuery();
         $subQueries[] = QueryUtils::getExecutableSql($subQuery);
         if (empty($selectStmt)) {
             $mapping = QueryUtils::parseQuery($subQuery)->getResultSetMapping();
             $selectStmt = sprintf('entity.%s AS id, entity.%s AS entity, entity.%s AS title', QueryUtils::getColumnNameByAlias($mapping, 'id'), QueryUtils::getColumnNameByAlias($mapping, 'entityClass'), QueryUtils::getColumnNameByAlias($mapping, 'entityTitle'));
         }
     }
     $rsm = new ResultSetMapping();
     $rsm->addScalarResult('id', 'id', Type::INTEGER)->addScalarResult('entity', 'entity')->addScalarResult('title', 'title');
     $queryBuilder = new SqlQueryBuilder($objectManager, $rsm);
     $queryBuilder->select($selectStmt)->from('(' . implode(' UNION ALL ', $subQueries) . ')', 'entity');
     return $queryBuilder;
 }
示例#8
0
 public function testNormalizeCriteriaArray()
 {
     $expectedCriteria = new Criteria();
     $expectedCriteria->andWhere(Criteria::expr()->eq('field', 'value'));
     $this->assertEquals($expectedCriteria, QueryUtils::normalizeCriteria(['field' => 'value']));
 }