Esempio n. 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;
 }
Esempio n. 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;
 }
Esempio n. 3
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;
 }