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