/**
  * @param CriteriaInterface|array $criteria
  * @return EntityInterface[]
  */
 public function findMany($criteria)
 {
     if (is_array($criteria)) {
         $criteria = $this->createCriteria($criteria);
     }
     if (empty($this->with)) {
         return $this->entityRepository->findMany($criteria);
     }
     /** @var Select $select */
     $select = $criteria->getQuery();
     $rows = $this->tableGateway->selectWith($select)->toArray();
     if (empty($rows)) {
         return [];
     }
     $relatedEntityIds = [];
     foreach ($rows as $row) {
         foreach ($this->with as $relatedEntityName => $cascadeWith) {
             $relatedField = $this->getRelatedField($relatedEntityName);
             if (!array_key_exists($relatedField, $row)) {
                 throw new \RuntimeException(get_class($this) . ": relation field {$relatedEntityName} not fetched");
             }
             if (!isset($relatedEntityIds[$relatedEntityName])) {
                 $relatedEntityIds[$relatedEntityName] = new ArrayObject();
             }
             if (!in_array($row[$relatedField], (array) $relatedEntityIds[$relatedEntityName])) {
                 $relatedEntityIds[$relatedEntityName]->append($row[$relatedField]);
             }
         }
     }
     $relatedEntities = [];
     foreach ($this->with as $relatedEntityName => $cascadeWith) {
         if (empty((array) $relatedEntityIds[$relatedEntityName])) {
             continue;
         }
         $criteria = $this->relatedRepository[$relatedEntityName]->createCriteria(['id.in' => (array) $relatedEntityIds[$relatedEntityName]]);
         $relatedEntities[$relatedEntityName] = $this->relatedRepository[$relatedEntityName]->findMany($criteria);
     }
     $entitiesArgs = [];
     foreach ($rows as &$row) {
         $entityArgs = ['data' => $this->mapper->fromTableRow($row)];
         foreach ($this->relationsConfig as $entityName => $joinRule) {
             $relatedField = $this->getRelatedField($entityName);
             if (isset($relatedEntities[$entityName])) {
                 if (isset($relatedEntities[$entityName][$row[$relatedField]])) {
                     $entityArgs['aggregateItems'][] = $relatedEntities[$entityName][$row[$relatedField]];
                 } else {
                     $entityArgs['aggregateItems'][] = null;
                 }
             } else {
                 $entityArgs['aggregateItems'][] = null;
             }
         }
         $entitiesArgs[] = $entityArgs;
     }
     $entities = $this->entityFactory->createCollection($entitiesArgs);
     $this->with = null;
     return $entities;
 }
示例#2
0
 /**
  * @dataProvider providerFromTableRow
  */
 public function testFromTableRow($row, $expectedRow)
 {
     $result = $this->mapper->fromTableRow($row);
     $this->assertEquals($expectedRow, $result);
 }