createCollection() public method

Allows wrap set of entities in custom collection
public createCollection ( array $entities ) : mixed
$entities array
return mixed
Exemplo n.º 1
0
 /**
  * Creates new set of Entity's instances from given array of \Dibi\Row instances
  *
  * @param \Dibi\Row[] $rows
  * @param string|null $entityClass
  * @param string|null $table
  * @return array
  */
 protected function createEntities(array $rows, $entityClass = null, $table = null)
 {
     if ($table === null) {
         $table = $this->getTable();
     }
     $entities = [];
     $collection = Result::createInstance($rows, $table, $this->connection, $this->mapper);
     $primaryKey = $this->mapper->getPrimaryKey($this->getTable());
     if ($entityClass !== null) {
         foreach ($rows as $dibiRow) {
             $entity = $this->entityFactory->createEntity($entityClass, $collection->getRow($dibiRow->{$primaryKey}));
             $entity->makeAlive($this->entityFactory);
             $entities[$dibiRow->{$primaryKey}] = $entity;
         }
     } else {
         foreach ($rows as $dibiRow) {
             $row = $collection->getRow($dibiRow->{$primaryKey});
             $entityClass = $this->mapper->getEntityClass($this->getTable(), $row);
             $entity = $this->entityFactory->createEntity($entityClass, $row);
             $entity->makeAlive($this->entityFactory);
             $entities[$dibiRow->{$primaryKey}] = $entity;
         }
     }
     return $this->entityFactory->createCollection($entities);
 }
Exemplo n.º 2
0
 /**
  * @param Property $property
  * @param Relationship\BelongsToMany $relationship micro-optimalization
  * @param Filtering|null $filtering
  * @return Entity[]
  */
 private function getBelongsToManyValue(Property $property, Relationship\BelongsToMany $relationship, Filtering $filtering = null)
 {
     $targetTable = $relationship->getTargetTable();
     $rows = $this->row->referencing($targetTable, $relationship->getColumnReferencingSourceTable(), $filtering, $relationship->getStrategy());
     $value = [];
     foreach ($rows as $row) {
         $entityClass = $this->mapper->getEntityClass($targetTable, $row);
         $entity = $this->entityFactory->createEntity($entityClass, $row);
         $this->checkConsistency($property, $entityClass, $entity);
         $entity->makeAlive($this->entityFactory);
         $value[] = $entity;
     }
     return $this->entityFactory->createCollection($value);
 }
Exemplo n.º 3
0
 /**
  * @return Entity[]
  */
 public function getEntities()
 {
     if ($this->entities === NULL) {
         $entities = array();
         $entityClass = $this->clauses->from['entityClass'];
         $result = $this->getResult($this->clauses->from['alias']);
         foreach ($result as $key => $row) {
             $entities[] = $entity = $this->entityFactory->createEntity($entityClass, new Row($result, $key));
             $entity->makeAlive($this->entityFactory, $this->connection, $this->mapper);
         }
         $this->entities = $this->entityFactory->createCollection($entities);
     }
     return $this->entities;
 }
Exemplo n.º 4
0
 /**
  * @return Entity[]
  */
 public function getEntities($limit = null, $offset = null)
 {
     if ($this->entities === NULL) {
         $entities = array();
         $entityClass = $this->clauses->from['entityClass'];
         $result = $this->getResult($this->clauses->from['alias'], $limit, $offset);
         $primaryKey = $this->mapper->getPrimaryKey($this->mapper->getTable($entityClass));
         foreach ($result as $key => $row) {
             $entity = $this->entityFactory->createEntity($entityClass, new Row($result, $key));
             $entities[$entity->{$primaryKey}] = $entity;
             $entity->makeAlive($this->entityFactory, $this->connection, $this->mapper);
             $entity->cleanReferencingRowsCache();
         }
         $this->entities = $this->entityFactory->createCollection($entities);
     }
     return $this->entities;
 }