LeanMapper\Result::createInstance PHP Method

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
    public static function createInstance($data, $table, Connection $connection, IMapper $mapper)
    {
        $dataArray = [];
        $primaryKey = $mapper->getPrimaryKey($table);
        if ($data instanceof DibiRow) {
            $dataArray = [isset($data->{$primaryKey}) ? $data->{$primaryKey} : self::DETACHED_ROW_ID => $data->toArray()];
        } else {
            $e = new InvalidArgumentException('Invalid type of data given, only \\Dibi\\Row, \\Dibi\\Row[], ArrayAccess[] or array of arrays is supported at this moment.');
            if (!is_array($data)) {
                throw $e;
            }
            if (!empty($data)) {
                $record = reset($data);
                if (!$record instanceof DibiRow and !is_array($record) and !$record instanceof ArrayAccess) {
                    throw $e;
                }
            }
            foreach ($data as $record) {
                $record = (array) $record;
                if (isset($record[$primaryKey])) {
                    $dataArray[$record[$primaryKey]] = $record;
                } else {
                    $dataArray[] = $record;
                }
            }
        }
        return new self($dataArray, $table, $connection, $mapper);
    }

Usage Example

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;
 }
All Usage Examples Of LeanMapper\Result::createInstance