Skip to content

Commit 03220b0

Browse files
committed
added save of delta instead of a full row overwrite
1 parent d9d27a8 commit 03220b0

File tree

2 files changed

+43
-38
lines changed

2 files changed

+43
-38
lines changed

src/Entity.php

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,32 @@
1010

1111
abstract class Entity extends \Wtf\Root
1212
{
13+
/**
14+
* map of relations.
15+
*
16+
* @var array
17+
*/
1318
protected $relationObjects = [];
19+
20+
/**
21+
* Original entity data, used to calculate delta
22+
* between original and updated data.
23+
*
24+
* @var array
25+
*/
26+
protected $__data;
27+
28+
/**
29+
* Database table scheme, to autostrip non-existing fields
30+
* from entity's data before save.
31+
*
32+
* @var array
33+
*/
1434
protected $scheme;
1535

1636
/**
1737
* Get short entity name (without namespace)
1838
* Helper function, required for lazy load.
19-
*
20-
* @return string
2139
*/
2240
protected function __getEntityName(): string
2341
{
@@ -26,9 +44,6 @@ protected function __getEntityName(): string
2644

2745
/**
2846
* Magic relation getter.
29-
*
30-
* @param null|string $method
31-
* @param array $params
3247
*/
3348
public function __call(?string $method = null, array $params = [])
3449
{
@@ -44,9 +59,21 @@ public function __call(?string $method = null, array $params = [])
4459
}
4560

4661
/**
47-
* Get entity scheme.
62+
* Set original data to entity.
4863
*
49-
* @return array
64+
* @NOTE: only for internal usage,
65+
*
66+
* @see \Wtf\Root::setData()
67+
*/
68+
protected function __setOriginalData(array $data): self
69+
{
70+
$this->__data = $data;
71+
72+
return $this;
73+
}
74+
75+
/**
76+
* Get entity scheme.
5077
*/
5178
public function getScheme(): array
5279
{
@@ -72,13 +99,13 @@ public function getScheme(): array
7299
/**
73100
* Save entity data in db.
74101
*
75-
* @param bool $validate
102+
* @param bool $delta Save only delta between original and changed data
76103
*
77104
* @throws Exception if entity data is not valid
78105
*
79106
* @return Entity
80107
*/
81-
public function save(bool $validate = true): self
108+
public function save(bool $validate = true, bool $delta = true): self
82109
{
83110
if ($validate && $this->validate()) {
84111
throw new Exception('Entity '.$this->__getEntityName().' data is not valid');
@@ -93,11 +120,12 @@ public function save(bool $validate = true): self
93120
unset($this->data[$key]);
94121
}
95122
}
123+
$data = $delta ? \array_diff_assoc($this->data ?? [], $this->__data ?? []) : $this->data;
96124

97125
if ($this->getId()) {
98-
$this->medoo->update($this->getTable(), $this->data, ['id' => $this->getId()]);
126+
$this->medoo->update($this->getTable(), $data, ['id' => $this->getId()]);
99127
} else {
100-
$this->medoo->insert($this->getTable(), $this->data);
128+
$this->medoo->insert($this->getTable(), $data);
101129
$this->setId($this->medoo->id());
102130
}
103131
$this->sentry->breadcrumbs->record([
@@ -143,9 +171,10 @@ public function validate(string $method = 'save'): array
143171
public function load($value, $field = 'id', array $fields = null): self
144172
{
145173
$data = $this->medoo->get($this->getTable(), $fields ?? '*', [$field => $value]);
146-
$this->data = \is_array($data) ? $data : []; //handle empty result gracefuly
174+
$data = \is_array($data) ? $data : []; //handle empty result gracefuly
175+
$this->__setOriginalData($data)->setData($data);
147176
$this->sentry->breadcrumbs->record([
148-
'message' => 'Entity '.$this->__getEntityName().'::load('.$value.', '.$field.', ['.\implode(', ', $fields ?? []).')',
177+
'message' => 'Entity '.$this->__getEntityName().'::load('.$value.', '.$field.', ['.\implode(', ', $fields ?? []).'])',
149178
'data' => ['query' => $this->medoo->last()],
150179
'category' => 'Database',
151180
'level' => 'info',
@@ -160,8 +189,6 @@ public function load($value, $field = 'id', array $fields = null): self
160189
* @param array $where Where clause
161190
* @param bool $assoc Return collection of entity objects OR of assoc arrays
162191
* @param array $fields Fields to load, default is all
163-
*
164-
* @return Collection
165192
*/
166193
public function loadAll(array $where = [], bool $assoc = false, array $fields = null): Collection
167194
{
@@ -174,7 +201,7 @@ public function loadAll(array $where = [], bool $assoc = false, array $fields =
174201
]);
175202
$items = [];
176203
foreach ($allData as $data) {
177-
$items[] = ($assoc) ? $data : $this->container['entity']($this->__getEntityName())->setData($data);
204+
$items[] = ($assoc) ? $data : $this->container['entity']($this->__getEntityName())->__setOriginalData($data)->setData($data);
178205
}
179206

180207
return new Collection($items);
@@ -208,10 +235,6 @@ public function loadRelation(string $name)
208235

209236
/**
210237
* Determine whether the target data existed.
211-
*
212-
* @param array $where
213-
*
214-
* @return bool
215238
*/
216239
public function has(array $where = []): bool
217240
{
@@ -222,8 +245,6 @@ public function has(array $where = []): bool
222245
* Get count of items by $where conditions.
223246
*
224247
* @param array $where Where clause
225-
*
226-
* @return int
227248
*/
228249
public function count(array $where = []): int
229250
{
@@ -232,8 +253,6 @@ public function count(array $where = []): int
232253

233254
/**
234255
* Delete entity row from db.
235-
*
236-
* @return bool
237256
*/
238257
public function delete(): bool
239258
{
@@ -242,8 +261,6 @@ public function delete(): bool
242261

243262
/**
244263
* Return entity table name.
245-
*
246-
* @return string
247264
*/
248265
abstract public function getTable(): string;
249266

@@ -259,8 +276,6 @@ abstract public function getTable(): string;
259276
* ];
260277
* </code>
261278
* Example: ['save' => ['name' => v::stringType()->length(1,255)]].
262-
*
263-
* @return array
264279
*/
265280
abstract public function getValidators(): array;
266281

@@ -304,8 +319,6 @@ abstract public function getValidators(): array;
304319
* ]
305320
* //This example can be called like $userEntity->getPosts()
306321
* </code>
307-
*
308-
* @return array
309322
*/
310323
abstract public function getRelations(): array;
311324
}

src/Provider.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ public function register(Container $container): void
2020

2121
/**
2222
* Set Medoo into container.
23-
*
24-
* @param Container $container
25-
*
26-
* @return callable
2723
*/
2824
protected function setMedoo(Container $container): callable
2925
{
@@ -36,10 +32,6 @@ protected function setMedoo(Container $container): callable
3632

3733
/**
3834
* Set entity() function into container.
39-
*
40-
* @param Container $container
41-
*
42-
* @return callable
4335
*/
4436
protected function setEntityLoader(Container $container): callable
4537
{

0 commit comments

Comments
 (0)