10
10
11
11
abstract class Entity extends \Wtf \Root
12
12
{
13
+ /**
14
+ * map of relations.
15
+ *
16
+ * @var array
17
+ */
13
18
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
+ */
14
34
protected $ scheme ;
15
35
16
36
/**
17
37
* Get short entity name (without namespace)
18
38
* Helper function, required for lazy load.
19
- *
20
- * @return string
21
39
*/
22
40
protected function __getEntityName (): string
23
41
{
@@ -26,9 +44,6 @@ protected function __getEntityName(): string
26
44
27
45
/**
28
46
* Magic relation getter.
29
- *
30
- * @param null|string $method
31
- * @param array $params
32
47
*/
33
48
public function __call (?string $ method = null , array $ params = [])
34
49
{
@@ -44,9 +59,21 @@ public function __call(?string $method = null, array $params = [])
44
59
}
45
60
46
61
/**
47
- * Get entity scheme .
62
+ * Set original data to entity .
48
63
*
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.
50
77
*/
51
78
public function getScheme (): array
52
79
{
@@ -72,13 +99,13 @@ public function getScheme(): array
72
99
/**
73
100
* Save entity data in db.
74
101
*
75
- * @param bool $validate
102
+ * @param bool $delta Save only delta between original and changed data
76
103
*
77
104
* @throws Exception if entity data is not valid
78
105
*
79
106
* @return Entity
80
107
*/
81
- public function save (bool $ validate = true ): self
108
+ public function save (bool $ validate = true , bool $ delta = true ): self
82
109
{
83
110
if ($ validate && $ this ->validate ()) {
84
111
throw new Exception ('Entity ' .$ this ->__getEntityName ().' data is not valid ' );
@@ -93,11 +120,12 @@ public function save(bool $validate = true): self
93
120
unset($ this ->data [$ key ]);
94
121
}
95
122
}
123
+ $ data = $ delta ? \array_diff_assoc ($ this ->data ?? [], $ this ->__data ?? []) : $ this ->data ;
96
124
97
125
if ($ this ->getId ()) {
98
- $ this ->medoo ->update ($ this ->getTable (), $ this -> data , ['id ' => $ this ->getId ()]);
126
+ $ this ->medoo ->update ($ this ->getTable (), $ data , ['id ' => $ this ->getId ()]);
99
127
} else {
100
- $ this ->medoo ->insert ($ this ->getTable (), $ this -> data );
128
+ $ this ->medoo ->insert ($ this ->getTable (), $ data );
101
129
$ this ->setId ($ this ->medoo ->id ());
102
130
}
103
131
$ this ->sentry ->breadcrumbs ->record ([
@@ -143,9 +171,10 @@ public function validate(string $method = 'save'): array
143
171
public function load ($ value , $ field = 'id ' , array $ fields = null ): self
144
172
{
145
173
$ 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 );
147
176
$ this ->sentry ->breadcrumbs ->record ([
148
- 'message ' => 'Entity ' .$ this ->__getEntityName ().'::load( ' .$ value .', ' .$ field .', [ ' .\implode (', ' , $ fields ?? []).') ' ,
177
+ 'message ' => 'Entity ' .$ this ->__getEntityName ().'::load( ' .$ value .', ' .$ field .', [ ' .\implode (', ' , $ fields ?? []).'] ) ' ,
149
178
'data ' => ['query ' => $ this ->medoo ->last ()],
150
179
'category ' => 'Database ' ,
151
180
'level ' => 'info ' ,
@@ -160,8 +189,6 @@ public function load($value, $field = 'id', array $fields = null): self
160
189
* @param array $where Where clause
161
190
* @param bool $assoc Return collection of entity objects OR of assoc arrays
162
191
* @param array $fields Fields to load, default is all
163
- *
164
- * @return Collection
165
192
*/
166
193
public function loadAll (array $ where = [], bool $ assoc = false , array $ fields = null ): Collection
167
194
{
@@ -174,7 +201,7 @@ public function loadAll(array $where = [], bool $assoc = false, array $fields =
174
201
]);
175
202
$ items = [];
176
203
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 );
178
205
}
179
206
180
207
return new Collection ($ items );
@@ -208,10 +235,6 @@ public function loadRelation(string $name)
208
235
209
236
/**
210
237
* Determine whether the target data existed.
211
- *
212
- * @param array $where
213
- *
214
- * @return bool
215
238
*/
216
239
public function has (array $ where = []): bool
217
240
{
@@ -222,8 +245,6 @@ public function has(array $where = []): bool
222
245
* Get count of items by $where conditions.
223
246
*
224
247
* @param array $where Where clause
225
- *
226
- * @return int
227
248
*/
228
249
public function count (array $ where = []): int
229
250
{
@@ -232,8 +253,6 @@ public function count(array $where = []): int
232
253
233
254
/**
234
255
* Delete entity row from db.
235
- *
236
- * @return bool
237
256
*/
238
257
public function delete (): bool
239
258
{
@@ -242,8 +261,6 @@ public function delete(): bool
242
261
243
262
/**
244
263
* Return entity table name.
245
- *
246
- * @return string
247
264
*/
248
265
abstract public function getTable (): string ;
249
266
@@ -259,8 +276,6 @@ abstract public function getTable(): string;
259
276
* ];
260
277
* </code>
261
278
* Example: ['save' => ['name' => v::stringType()->length(1,255)]].
262
- *
263
- * @return array
264
279
*/
265
280
abstract public function getValidators (): array ;
266
281
@@ -304,8 +319,6 @@ abstract public function getValidators(): array;
304
319
* ]
305
320
* //This example can be called like $userEntity->getPosts()
306
321
* </code>
307
- *
308
- * @return array
309
322
*/
310
323
abstract public function getRelations (): array ;
311
324
}
0 commit comments