/** * @param string $definition * @param string $alias * @param string $type * @throws InvalidArgumentException */ public function addJoinByType($definition, $alias, $type) { list($fromAlias, $viaProperty) = $this->parseDotNotation($definition); $entityReflection = $this->getReflection($fromEntity = $this->aliases->getEntityClass($fromAlias)); $property = $entityReflection->getEntityProperty($viaProperty); if (!$property->hasRelationship()) { throw new InvalidArgumentException(); } $relationship = $property->getRelationship(); if ($relationship instanceof HasMany) { $this->clauses->join[] = array('type' => $type, 'joinParameters' => array($relationshipTable = $relationship->getRelationshipTable(), $relTableAlias = $relationshipTable . $this->indexer), 'onParameters' => array($fromAlias, $primaryKey = $this->mapper->getPrimaryKey($fromTable = $this->mapper->getTable($fromEntity)), $relTableAlias, $columnReferencingSourceTable = $relationship->getColumnReferencingSourceTable())); $this->hydratorMeta->addTablePrefix($relTableAlias, $relationshipTable); $this->hydratorMeta->addPrimaryKey($relationshipTable, $relTablePrimaryKey = $this->mapper->getPrimaryKey($relationshipTable)); $this->hydratorMeta->addRelationship($alias, new Relationship($fromAlias, $fromTable, $columnReferencingSourceTable, Relationship::DIRECTION_REFERENCING, $relTableAlias, $relationshipTable, $primaryKey)); $this->clauses->join[] = array('type' => $type, 'joinParameters' => array($targetTable = $relationship->getTargetTable(), $alias), 'onParameters' => array($relTableAlias, $columnReferencingTargetTable = $relationship->getColumnReferencingTargetTable(), $alias, $primaryKey = $this->mapper->getPrimaryKey($targetTable))); $this->aliases->addAlias($alias, $property->getType()); $this->hydratorMeta->addTablePrefix($alias, $targetTable); $this->hydratorMeta->addPrimaryKey($targetTable, $primaryKey); $this->hydratorMeta->addRelationship($relTableAlias, new Relationship($relTableAlias, $relationshipTable, $columnReferencingTargetTable, Relationship::DIRECTION_REFERENCED, $alias, $targetTable, $primaryKey)); $this->relationshipTables[$alias] = array($relTableAlias, $relTablePrimaryKey, $relTableAlias . QueryHelper::PREFIX_SEPARATOR . $relTablePrimaryKey, $relTableAlias, $columnReferencingSourceTable, $relTableAlias . QueryHelper::PREFIX_SEPARATOR . $columnReferencingSourceTable, $relTableAlias, $columnReferencingTargetTable, $relTableAlias . QueryHelper::PREFIX_SEPARATOR . $columnReferencingTargetTable); $this->indexer++; } else { $this->clauses->join[] = array('type' => $type, 'joinParameters' => array($targetTable = $relationship->getTargetTable(), $alias), 'onParameters' => $relationship instanceof HasOne ? array($fromAlias, $relationshipColumn = $relationship->getColumnReferencingTargetTable(), $alias, $primaryKey = $this->mapper->getPrimaryKey($targetTable)) : array($fromAlias, $primaryKey = $this->mapper->getPrimaryKey($fromTable = $this->mapper->getTable($fromEntity)), $alias, $columnReferencingSourceTable = $relationship->getColumnReferencingSourceTable())); $this->aliases->addAlias($alias, $property->getType()); $this->hydratorMeta->addTablePrefix($alias, $targetTable); if ($relationship instanceof HasOne) { $this->hydratorMeta->addPrimaryKey($targetTable, $primaryKey); $this->hydratorMeta->addRelationship($alias, new Relationship($fromAlias, $this->mapper->getTable($fromEntity), $relationshipColumn, Relationship::DIRECTION_REFERENCED, $alias, $targetTable, $primaryKey)); } else { $this->hydratorMeta->addPrimaryKey($targetTable, $targetTablePrimaryKey = $this->mapper->getPrimaryKey($targetTable)); $this->hydratorMeta->addRelationship($fromAlias, new Relationship($fromAlias, $fromTable, $columnReferencingSourceTable, Relationship::DIRECTION_REFERENCING, $alias, $targetTable, $primaryKey)); } } }
/** * @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; }