/** * @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; }
/** * 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; }