createInstance() public static method

Creates new common instance (it means persisted)
public static createInstance ( Row | Row[] $data, string $table, Connection $connection, leanmapper\IMapper $mapper ) : self
$data Dibi\Row | Dibi\Row[]
$table string
$connection Connection
$mapper leanmapper\IMapper
return self
Example #1
0
 /**
  * @param DibiRow[] $data
  * @param HydratorMeta $hydratorMeta
  * @param array|null $relationshipsFilter
  * @return array
  */
 public function buildResultsGraph(array $data, HydratorMeta $hydratorMeta, array $relationshipsFilter = null)
 {
     $results = array_fill_keys(array_keys($hydratorMeta->getTablesByPrefixes()), array());
     $index = array();
     foreach ($data as $row) {
         $currentPrimaryKeys = array();
         foreach ($hydratorMeta->getTablesByPrefixes() as $prefix => $table) {
             $alias = $prefix . QueryHelper::PREFIX_SEPARATOR . $hydratorMeta->getPrimaryKeyByTable($table);
             if (isset($row[$alias])) {
                 $currentPrimaryKeys[$prefix] = $row[$alias];
             }
         }
         foreach ($row as $field => $value) {
             if (!isset($index[$field])) {
                 $index[$field] = explode(QueryHelper::PREFIX_SEPARATOR, $field, 2);
             }
             list($prefix, $field) = $index[$field];
             if (!isset($results[$prefix]) or !isset($currentPrimaryKeys[$prefix]) or isset($results[$prefix][$currentPrimaryKeys[$prefix]][$field])) {
                 continue;
             }
             if (!isset($results[$prefix][$currentPrimaryKeys[$prefix]])) {
                 $results[$prefix][$currentPrimaryKeys[$prefix]] = array();
             }
             $results[$prefix][$currentPrimaryKeys[$prefix]][$field] = $value;
         }
     }
     foreach ($results as $prefix => $rows) {
         $results[$prefix] = Result::createInstance($rows, $hydratorMeta->getTableByPrefix($prefix), $this->connection, $this->mapper);
     }
     $relationships = $hydratorMeta->getRelationships($relationshipsFilter);
     if (!empty($relationships)) {
         $this->linkResults($results, $relationships);
     }
     return $results;
 }
Example #2
0
 public function preloadEdges(ResultProxy $resultProxy)
 {
     $nodesIds = array();
     foreach ($resultProxy as $node) {
         $nodesIds[$node['id']] = true;
     }
     $edges = $this->connection->select('*')->from('edge')->where('[source] IN %in OR [target] IN %in', $ids = array_keys($nodesIds), $ids)->orderBy('type')->fetchAll();
     $referencing = Result::createInstance(array(), 'edge', $this->connection, $this->mapper);
     foreach ($edges as $edge) {
         if (isset($nodesIds[$edge['source']]) || isset($nodesIds[$edge['target']])) {
             $edge = $edge->toArray();
             $edge['related_node'] = $edge['source'];
             $referencing->addDataEntry($edge);
             if ($edge['target'] !== $edge['source'] and $edge['target'] !== null) {
                 $edge['related_node'] = $edge['target'];
                 $referencing->addDataEntry($edge);
             }
         }
     }
     $referencing->cleanAddedAndRemovedMeta();
     $resultProxy->setReferencingResult($referencing, 'edge', 'related_node');
 }
Example #3
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);
 }