Skip to content

Commit

Permalink
Implemented Result::setReferencedResult and Result::setReferencingRes…
Browse files Browse the repository at this point in the history
…ult methods
  • Loading branch information
Tharos committed Apr 9, 2014
1 parent 94f56c1 commit 3de9fd3
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions LeanMapper/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class Result implements \Iterator

const DETACHED_ROW_ID = -1;

const PRELOADED_KEY = 'preloaded';

/** @var bool */
private $isDetached;

Expand Down Expand Up @@ -618,6 +620,9 @@ private function getReferencedResult($table, $viaColumn, Filtering $filtering =
throw new InvalidStateException('Cannot get referenced Result for detached Result.');
}
$key = "$table($viaColumn)";
if (isset($this->referenced[$preloadedKey = $key . '#' . self::PRELOADED_KEY])) {
return $this->referenced[$preloadedKey];
}
$primaryKey = $this->mapper->getPrimaryKey($table);
if ($filtering === null) {
if (!isset($this->referenced[$key])) {
Expand All @@ -641,6 +646,19 @@ private function getReferencedResult($table, $viaColumn, Filtering $filtering =
return $this->referenced[$key];
}

/**
* @param self $referencedResult
* @param string $table
* @param string $viaColumn
*/
public function setReferencedResult(self $referencedResult, $table, $viaColumn = null)
{
if ($viaColumn === null) {
$viaColumn = $this->mapper->getRelationshipColumn($table, $this->table);
}
$this->referenced["$table($viaColumn)#" . self::PRELOADED_KEY] = $referencedResult;
}

/**
* @param string $table
* @param string $viaColumn
Expand All @@ -660,6 +678,9 @@ private function getReferencingResult($table, $viaColumn = null, Filtering $filt
$viaColumn = $this->mapper->getRelationshipColumn($table, $this->table);
}
$key = "$table($viaColumn)$strategy";
if (isset($this->referencing[$preloadedKey = $key . '#' . self::PRELOADED_KEY])) {
return $this->referencing[$preloadedKey];
}
$primaryKey = $this->mapper->getPrimaryKey($this->table);
if ($strategy === self::STRATEGY_IN) {
if ($filtering === null) {
Expand Down Expand Up @@ -725,6 +746,22 @@ private function getReferencingResult($table, $viaColumn = null, Filtering $filt
return $this->referencing[$key];
}

/**
* @param Result $referencingResult
* @param string $table
* @param string $viaColumn
* @param string $strategy
*/
public function setReferencingResult(self $referencingResult, $table, $viaColumn = null, $strategy = self::STRATEGY_IN)
{
$strategy = $this->translateStrategy($strategy);
if ($viaColumn === null) {
$viaColumn = $this->mapper->getRelationshipColumn($table, $this->table);
}
$this->referencing["$table($viaColumn)$strategy#" . self::PRELOADED_KEY] = $referencingResult;
unset($this->index[$referencingResult->getOriginKey()]);
}

/**
* @param string $column
* @return array
Expand Down

0 comments on commit 3de9fd3

Please sign in to comment.