/** * @param array $associationMapping * @param array $entityIds * * @return QueryBuilder */ public function getToManyAssociationQueryBuilder($associationMapping, $entityIds) { $entityIdField = $this->doctrineHelper->getEntityIdFieldName($associationMapping['sourceEntity']); $qb = $this->doctrineHelper->getEntityRepository($associationMapping['targetEntity'])->createQueryBuilder('r')->select(sprintf('e.%s as entityId', $entityIdField)); if (count($entityIds) === 1) { $qb->where(sprintf('e.%s = :id', $entityIdField))->setParameter('id', reset($entityIds)); } else { $qb->where(sprintf('e.%s IN (:ids)', $entityIdField))->setParameter('ids', $entityIds); } if ($associationMapping['mappedBy'] && $associationMapping['type'] === ClassMetadata::ONE_TO_MANY) { $qb->innerJoin($associationMapping['sourceEntity'], 'e', 'WITH', sprintf('r.%s = e', $associationMapping['mappedBy'])); } else { $qb->innerJoin($associationMapping['sourceEntity'], 'e', 'WITH', sprintf('r MEMBER OF e.%s', $associationMapping['fieldName'])); } return $qb; }
/** * @param array $entityIds * @param array $mapping * @param array $config * * @return array */ protected function loadRelatedItems($entityIds, $mapping, $config) { $entityClass = $mapping['targetEntity']; $bindings = $this->getRelatedItemsBindings($entityIds, $mapping, $config); $qb = $this->doctrineHelper->getEntityRepository($entityClass)->createQueryBuilder('r')->where(sprintf('r.%s IN (:ids)', $this->getEntityIdFieldName($entityClass)))->setParameter('ids', $this->getRelatedItemsIds($bindings)); $this->prepareQuery($qb, $config); $data = $this->getQuery($qb, $config)->getResult(); $result = []; if (!empty($data)) { $items = $this->serializeEntities((array) $data, $entityClass, $config, true); foreach ($bindings as $entityId => $relatedEntityIds) { foreach ($relatedEntityIds as $relatedEntityId) { if (isset($items[$relatedEntityId])) { $result[$entityId][] = $items[$relatedEntityId]; } } } } return $result; }