Skip to content

Commit 43fecb7

Browse files
committed
Result: added possibility to customize isModified behavior
1 parent 1d4decb commit 43fecb7

File tree

2 files changed

+116
-2
lines changed

2 files changed

+116
-2
lines changed

src/LeanMapper/Result.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public static function createInstance($data, $table, Connection $connection, IMa
121121
}
122122
}
123123
}
124-
return new self($dataArray, $table, $connection, $mapper);
124+
return new static($dataArray, $table, $connection, $mapper);
125125
}
126126

127127

@@ -133,7 +133,7 @@ public static function createInstance($data, $table, Connection $connection, IMa
133133
*/
134134
public static function createDetachedInstance()
135135
{
136-
return new self;
136+
return new static;
137137
}
138138

139139

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
use Tester\Assert;
4+
5+
require_once __DIR__ . '/../bootstrap.php';
6+
7+
class CustomResult extends LeanMapper\Result
8+
{
9+
public function setDataEntry($id, $key, $value)
10+
{
11+
if (!$this->hasDataEntry($id, $key) || $this->getDataEntry($id, $key) !== $value) {
12+
parent::setDataEntry($id, $key, $value);
13+
}
14+
}
15+
}
16+
17+
18+
class CustomRepository extends LeanMapper\Repository
19+
{
20+
public function find($id)
21+
{
22+
$row = $this->connection->select('*')->from($this->getTable())->where('id = %i', $id)->fetch();
23+
if ($row === false) {
24+
throw new \Exception('Entity was not found.');
25+
}
26+
return $this->createEntity($row);
27+
}
28+
29+
30+
protected function createEntity(Dibi\Row $dibiRow, $entityClass = null, $table = null)
31+
{
32+
if ($table === null) {
33+
$table = $this->getTable();
34+
}
35+
$result = CustomResult::createInstance($dibiRow, $table, $this->connection, $this->mapper);
36+
$primaryKey = $this->mapper->getPrimaryKey($table);
37+
38+
$row = $result->getRow($dibiRow->$primaryKey);
39+
if ($entityClass === null) {
40+
$entityClass = $this->mapper->getEntityClass($table, $row);
41+
}
42+
$entity = $this->entityFactory->createEntity($entityClass, $row);
43+
$entity->makeAlive($this->entityFactory);
44+
return $entity;
45+
}
46+
47+
48+
// TODO: Repository::createEntities() && Entity::__construct()
49+
}
50+
51+
52+
/**
53+
* @property int $id
54+
* @property string $name
55+
*/
56+
class Author extends LeanMapper\Entity
57+
{
58+
}
59+
60+
61+
/**
62+
* @property int $id
63+
* @property string $name
64+
* @property Author $author m:hasOne
65+
*/
66+
class Book extends LeanMapper\Entity
67+
{
68+
}
69+
70+
71+
class AuthorRepository extends CustomRepository
72+
{
73+
}
74+
75+
76+
class BookRepository extends CustomRepository
77+
{
78+
}
79+
80+
81+
$authorRepository = new AuthorRepository($connection, $mapper, $entityFactory);
82+
$bookRepository = new BookRepository($connection, $mapper, $entityFactory);
83+
84+
//////////
85+
86+
$book = $bookRepository->find(3);
87+
88+
//////////
89+
90+
$oldName = $book->name;
91+
$oldAuthor = $book->author;
92+
93+
$book->name = $oldName;
94+
$book->author = $oldAuthor;
95+
96+
Assert::same(FALSE, $book->isModified());
97+
98+
//////////
99+
100+
$book->name = 'new book name';
101+
$book->author = $authorRepository->find(1);
102+
103+
Assert::same(TRUE, $book->isModified());
104+
105+
//////////
106+
107+
$newBook = new Book;
108+
109+
Assert::same(FALSE, $newBook->isModified());
110+
111+
$newBook->name = 'new book name #2';
112+
$newBook->author = $authorRepository->find(1);
113+
114+
Assert::same(TRUE, $newBook->isModified());

0 commit comments

Comments
 (0)