Skip to content
Permalink
Browse files

Repository: createEntities() - uses $table from parameters

  • Loading branch information...
janpecha committed May 14, 2019
1 parent 5e4d1aa commit 4f4f9bf1eaec31b295219406550f5ddad28381cd
@@ -366,7 +366,7 @@ protected function createEntities(array $rows, $entityClass = null, $table = nul
} else {
foreach ($rows as $dibiRow) {
$row = $collection->getRow($dibiRow->$primaryKey);
$entityClass = $this->mapper->getEntityClass($this->getTable(), $row);
$entityClass = $this->mapper->getEntityClass($table, $row);
$entity = $this->entityFactory->createEntity($entityClass, $row);
$entity->makeAlive($this->entityFactory);
$entities[$dibiRow->$primaryKey] = $entity;
@@ -0,0 +1,127 @@
<?php
use LeanMapper\Repository;
use Tester\Assert;
require_once __DIR__ . '/../bootstrap.php';
//////////
/**
* @property int $id
* @property string $name
* @property string|null $web
*/
class Author extends LeanMapper\Entity
{
}
/**
* @property int $id
* @property string $name
*/
class Book extends LeanMapper\Entity
{
}
class LibraryRepository extends Repository
{
public function findAuthor($id)
{
return $this->find('author', $id);
}
public function findBook($id)
{
return $this->find('book', $id);
}
public function findAuthors()
{
return $this->findAll('author');
}
public function findBooks()
{
return $this->findAll('book');
}
private function find($table, $id)
{
$row = $this->connection->select('*')->from($table)->where('id = %i', $id)->fetch();
if ($row === false) {
throw new \Exception('Entity was not found.');
}
return $this->createEntity($row, null, $table);
}
private function findAll($table)
{
return $this->createEntities(
$this->connection->select('*')->from($table)->orderBy('id')->fetchAll(),
null,
$table
);
}
}
//////////
$repository = new LibraryRepository($connection, $mapper, $entityFactory);
$authors = $repository->findAuthors();
$names = [];
foreach ($authors as $author) {
Assert::type('Author', $author);
$names[] = $author->name;
}
Assert::same([
'Andrew Hunt',
'Donald Knuth',
'Martin Fowler',
'Kent Beck',
'Thomas H. Cormen',
], $names);
//////////
$books = $repository->findBooks();
$names = [];
foreach ($books as $book) {
Assert::type('Book', $book);
$names[] = $book->name;
}
Assert::same([
'The Pragmatic Programmer',
'The Art of Computer Programming',
'Refactoring: Improving the Design of Existing Code',
'Introduction to Algorithms',
'UML Distilled',
], $names);
//////////
$author = $repository->findAuthor(3);
Assert::type('Author', $author);
Assert::same(3, $author->id);
Assert::same('Martin Fowler', $author->name);
//////////
$book = $repository->findBook(2);
Assert::type('Book', $book);
Assert::same(2, $book->id);
Assert::same('The Art of Computer Programming', $book->name);
@@ -0,0 +1,129 @@
<?php
use LeanMapper\Repository;
use Tester\Assert;
require_once __DIR__ . '/../bootstrap.php';
//////////
/**
* @property int $id
* @property string $name
*/
class Author extends LeanMapper\Entity
{
}
/**
* @property int $id
* @property string $name
*/
class Book extends LeanMapper\Entity
{
}
/**
* @property Author $reviewer m:hasOne
*/
class ReviewedBook extends Book
{
}
class LibraryRepository extends Repository
{
public function findReviewedBook($id)
{
$row = $this->connection->select('*')->from('book')->where('id = %i', $id)->where('reviewer_id IS NOT NULL')->fetch();
if ($row === false) {
throw new \Exception('Entity was not found.');
}
return $this->createEntity($row, 'ReviewedBook', 'book');
}
public function findBook($id)
{
$row = $this->connection->select('*')->from('book')->where('id = %i', $id)->fetch();
if ($row === false) {
throw new \Exception('Entity was not found.');
}
return $this->createEntity($row, 'Book', 'book');
}
public function findReviewedBooks()
{
return $this->createEntities(
$this->connection->select('*')->from('book')->where('reviewer_id IS NOT NULL')->orderBy('id')->fetchAll(),
'ReviewedBook',
'book'
);
}
public function findBooks()
{
return $this->createEntities(
$this->connection->select('*')->from('book')->orderBy('id')->fetchAll(),
'Book',
'book'
);
}
}
//////////
$repository = new LibraryRepository($connection, $mapper, $entityFactory);
$reviewedBooks = $repository->findReviewedBooks();
$names = [];
foreach ($reviewedBooks as $reviewedBook) {
Assert::type('ReviewedBook', $reviewedBook);
$names[] = $reviewedBook->name;
}
Assert::same([
'The Art of Computer Programming',
'Refactoring: Improving the Design of Existing Code',
], $names);
//////////
$books = $repository->findBooks();
$names = [];
foreach ($books as $book) {
Assert::type('Book', $book);
$names[] = $book->name;
}
Assert::same([
'The Pragmatic Programmer',
'The Art of Computer Programming',
'Refactoring: Improving the Design of Existing Code',
'Introduction to Algorithms',
'UML Distilled',
], $names);
//////////
$reviewedBook = $repository->findReviewedBook(3);
Assert::type('ReviewedBook', $reviewedBook);
Assert::same(3, $reviewedBook->id);
Assert::same('Refactoring: Improving the Design of Existing Code', $reviewedBook->name);
//////////
$book = $repository->findBook(2);
Assert::type('Book', $book);
Assert::same(2, $book->id);
Assert::same('The Art of Computer Programming', $book->name);

0 comments on commit 4f4f9bf

Please sign in to comment.
You can’t perform that action at this time.