Permalink
Browse files

Merge branch 'preloaded' into develop

  • Loading branch information...
Tharos committed Apr 9, 2014
2 parents 94f56c1 + 4d3d907 commit f21c9f7898633ece4ac30fdc9b73f43824a6d09d
Showing with 142 additions and 0 deletions.
  1. +37 −0 LeanMapper/Result.php
  2. +105 −0 tests/LeanMapper/Preloading.phpt
View
@@ -33,6 +33,8 @@ class Result implements \Iterator
const DETACHED_ROW_ID = -1;
const PRELOADED_KEY = 'preloaded';
/** @var bool */
private $isDetached;
@@ -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])) {
@@ -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
@@ -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) {
@@ -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
@@ -0,0 +1,105 @@
<?php
use LeanMapper\Connection;
use LeanMapper\DefaultMapper;
use LeanMapper\Entity;
use LeanMapper\Result;
use Tester\Assert;
require_once __DIR__ . '/../bootstrap.php';
$i = 1;
$connection->onEvent[] = function ($event) use (&$queries, &$i) {
$queries[] = $event->sql;
$i++;
};
//////////
class Mapper extends DefaultMapper
{
protected $defaultEntityNamespace = null;
}
/**
* @property int $id
* @property string $name
* @property Book[] $books m:belongsToMany
*/
class Author extends Entity
{
}
/**
* @property int $id
* @property string $name
*/
class Book extends Entity
{
}
//////////
$result = $connection->select('[author.id] [author_id], [author.name] [author_name], [book.id] [book_id], [book.name] [book_name], [book.author_id] [book_author_id]')
->from('author')
->join('book')->on('[book.author_id] = [author.id]')
->where('[book.name] != %s', 'The Pragmatic Programmer')
->where('LENGTH([book].[name]) > %i', 13)
->fetchAll();
$authors = [];
$books = [];
foreach ($result as $row) {
if (!isset($authors[$row['author_id']])) {
$authors[$row['author_id']] = new DibiRow([
'id' => $row['author_id'],
'name' => $row['author_name'],
]);
}
if (!isset($books[$row['book_id']])) {
$books[$row['book_id']] = new DibiRow([
'id' => $row['book_id'],
'name' => $row['book_name'],
'author_id' => $row['book_author_id'],
]);
}
}
$authorsResult = Result::createInstance($authors, 'author', $connection, $mapper);
$booksResult = Result::createInstance($books, 'book', $connection, $mapper);
$authorsResult->setReferencingResult($booksResult, 'book', 'author_id');
$entities = [];
foreach ($authors as $author) {
$entity = $entityFactory->createEntity(Author::class, $authorsResult->getRow($author->id));
$entity->makeAlive($entityFactory);
$entities[$author->id] = $entity;
}
$authors = $entityFactory->createCollection($entities);
//////////
$output = array();
foreach ($authors as $author) {
$outputBooks = array();
foreach ($author->books as $book) {
$outputBooks[] = $book->name;
}
$output[$author->name] = $outputBooks;
}
Assert::equal(array(
'Donald Knuth' => array('The Art of Computer Programming'),
'Martin Fowler' => array('Refactoring: Improving the Design of Existing Code'),
'Thomas H. Cormen' => array('Introduction to Algorithms'),
), $output);
Assert::equal(
"SELECT [author].[id] [author_id], [author].[name] [author_name], [book].[id] [book_id], [book].[name] [book_name], [book].[author_id] [book_author_id] FROM [author] JOIN [book] ON [book].[author_id] = [author].[id] WHERE [book].[name] != 'The Pragmatic Programmer' AND LENGTH([book].[name]) > 13",
reset($queries)
);

0 comments on commit f21c9f7

Please sign in to comment.