|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ORM; |
|
4
|
|
|
|
|
5
|
|
|
use ORM\Dbal\Column; |
|
6
|
|
|
use ORM\Dbal\Error; |
|
7
|
|
|
use ORM\Dbal\Table; |
|
8
|
|
|
use ORM\EntityManager as EM; |
|
9
|
|
|
use ORM\Exception\IncompletePrimaryKey; |
|
10
|
|
|
use ORM\Exception\InvalidConfiguration; |
|
11
|
|
|
use ORM\Exception\InvalidName; |
|
12
|
|
|
use ORM\Exception\InvalidRelation; |
|
13
|
|
|
use ORM\Exception\NoEntityManager; |
|
14
|
|
|
use ORM\Exception\UndefinedRelation; |
|
15
|
|
|
use ORM\Exception\UnknownColumn; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Definition of an entity |
|
19
|
|
|
* |
|
20
|
|
|
* The instance of an entity represents a row of the table and the statics variables and methods describe the database |
|
21
|
|
|
* table. |
|
22
|
|
|
* |
|
23
|
|
|
* This is the main part where your configuration efforts go. The following properties and methods are well documented |
|
24
|
|
|
* in the manual under [https://tflori.github.io/orm/entityDefinition.html](Entity Definition). |
|
25
|
|
|
* |
|
26
|
|
|
* @package ORM |
|
27
|
|
|
* @link https://tflori.github.io/orm/entityDefinition.html Entity Definition |
|
28
|
|
|
* @author Thomas Flori <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
abstract class Entity implements \Serializable |
|
31
|
|
|
{ |
|
32
|
|
|
/** @deprecated Use Relation::OPT_CLASS instead */ |
|
33
|
|
|
const OPT_RELATION_CLASS = 'class'; |
|
34
|
|
|
/** @deprecated Use Relation::OPT_CARDINALITY instead */ |
|
35
|
|
|
const OPT_RELATION_CARDINALITY = 'cardinality'; |
|
36
|
|
|
/** @deprecated Use Relation::OPT_REFERENCE instead */ |
|
37
|
|
|
const OPT_RELATION_REFERENCE = 'reference'; |
|
38
|
|
|
/** @deprecated Use Relation::OPT_OPPONENT instead */ |
|
39
|
|
|
const OPT_RELATION_OPPONENT = 'opponent'; |
|
40
|
|
|
/** @deprecated Use Relation::OPT_TABLE instead */ |
|
41
|
|
|
const OPT_RELATION_TABLE = 'table'; |
|
42
|
|
|
|
|
43
|
|
|
/** The template to use to calculate the table name. |
|
44
|
|
|
* @var string */ |
|
45
|
|
|
protected static $tableNameTemplate; |
|
46
|
|
|
|
|
47
|
|
|
/** The naming scheme to use for table names. |
|
48
|
|
|
* @var string */ |
|
49
|
|
|
protected static $namingSchemeTable; |
|
50
|
|
|
|
|
51
|
|
|
/** The naming scheme to use for column names. |
|
52
|
|
|
* @var string */ |
|
53
|
|
|
protected static $namingSchemeColumn; |
|
54
|
|
|
|
|
55
|
|
|
/** The naming scheme to use for method names. |
|
56
|
|
|
* @var string */ |
|
57
|
|
|
protected static $namingSchemeMethods; |
|
58
|
|
|
|
|
59
|
|
|
/** Fixed table name (ignore other settings) |
|
60
|
|
|
* @var string */ |
|
61
|
|
|
protected static $tableName; |
|
62
|
|
|
|
|
63
|
|
|
/** The variable(s) used for primary key. |
|
64
|
|
|
* @var string[]|string */ |
|
65
|
|
|
protected static $primaryKey = ['id']; |
|
66
|
|
|
|
|
67
|
|
|
/** Fixed column names (ignore other settings) |
|
68
|
|
|
* @var string[] */ |
|
69
|
|
|
protected static $columnAliases = []; |
|
70
|
|
|
|
|
71
|
|
|
/** A prefix for column names. |
|
72
|
|
|
* @var string */ |
|
73
|
|
|
protected static $columnPrefix; |
|
74
|
|
|
|
|
75
|
|
|
/** Whether or not the primary key is auto incremented. |
|
76
|
|
|
* @var bool */ |
|
77
|
|
|
protected static $autoIncrement = true; |
|
78
|
|
|
|
|
79
|
|
|
/** Whether or not the validator for this class is enabled. |
|
80
|
|
|
* @var bool */ |
|
81
|
|
|
protected static $enableValidator = false; |
|
82
|
|
|
|
|
83
|
|
|
/** Whether or not the validator for a class got enabled during runtime. |
|
84
|
|
|
* @var bool[] */ |
|
85
|
|
|
protected static $enabledValidators = []; |
|
86
|
|
|
|
|
87
|
|
|
/** Relation definitions |
|
88
|
|
|
* @var array */ |
|
89
|
|
|
protected static $relations = []; |
|
90
|
|
|
|
|
91
|
|
|
/** The reflections of the classes. |
|
92
|
|
|
* @internal |
|
93
|
|
|
* @var \ReflectionClass[] */ |
|
94
|
|
|
protected static $reflections = []; |
|
95
|
|
|
|
|
96
|
|
|
/** The current data of a row. |
|
97
|
|
|
* @var mixed[] */ |
|
98
|
|
|
protected $data = []; |
|
99
|
|
|
|
|
100
|
|
|
/** The original data of the row. |
|
101
|
|
|
* @var mixed[] */ |
|
102
|
|
|
protected $originalData = []; |
|
103
|
|
|
|
|
104
|
|
|
/** The entity manager from which this entity got created |
|
105
|
|
|
* @var EM */ |
|
106
|
|
|
protected $entityManager; |
|
107
|
|
|
|
|
108
|
|
|
/** Related objects for getRelated |
|
109
|
|
|
* @var array */ |
|
110
|
|
|
protected $relatedObjects = []; |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Constructor |
|
114
|
|
|
* |
|
115
|
|
|
* It calls ::onInit() after initializing $data and $originalData. |
|
116
|
|
|
* |
|
117
|
|
|
* @param mixed[] $data The current data |
|
118
|
|
|
* @param EM $entityManager The EntityManager that created this entity |
|
119
|
|
|
* @param bool $fromDatabase Whether or not the data comes from database |
|
120
|
|
|
*/ |
|
121
|
121 |
|
final public function __construct(array $data = [], EM $entityManager = null, $fromDatabase = false) |
|
122
|
|
|
{ |
|
123
|
121 |
|
if ($fromDatabase) { |
|
124
|
14 |
|
$this->originalData = $data; |
|
125
|
|
|
} |
|
126
|
121 |
|
$this->data = array_merge($this->data, $data); |
|
127
|
121 |
|
$this->entityManager = $entityManager ?: EM::getInstance(static::class); |
|
128
|
121 |
|
$this->onInit(!$fromDatabase); |
|
129
|
121 |
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Get a description for this table. |
|
133
|
|
|
* |
|
134
|
|
|
* @return Table|Column[] |
|
135
|
|
|
* @codeCoverageIgnore This is just a proxy |
|
136
|
|
|
*/ |
|
137
|
|
|
public static function describe() |
|
138
|
|
|
{ |
|
139
|
|
|
return EM::getInstance(static::class)->describe(static::getTableName()); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Get the column name of $attribute |
|
144
|
|
|
* |
|
145
|
|
|
* The column names can not be specified by template. Instead they are constructed by $columnPrefix and enforced |
|
146
|
|
|
* to $namingSchemeColumn. |
|
147
|
|
|
* |
|
148
|
|
|
* **ATTENTION**: If your overwrite this method remember that getColumnName(getColumnName($name)) have to be exactly |
|
149
|
|
|
* the same as getColumnName($name). |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $attribute |
|
152
|
|
|
* @return string |
|
153
|
|
|
* @throws InvalidConfiguration |
|
154
|
|
|
*/ |
|
155
|
165 |
|
public static function getColumnName($attribute) |
|
156
|
|
|
{ |
|
157
|
165 |
|
if (isset(static::$columnAliases[$attribute])) { |
|
158
|
6 |
|
return static::$columnAliases[$attribute]; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
163 |
|
return EM::getInstance(static::class)->getNamer() |
|
162
|
163 |
|
->getColumnName(static::class, $attribute, static::$columnPrefix, static::$namingSchemeColumn); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Get the primary key vars |
|
167
|
|
|
* |
|
168
|
|
|
* The primary key can consist of multiple columns. You should configure the vars that are translated to these |
|
169
|
|
|
* columns. |
|
170
|
|
|
* |
|
171
|
|
|
* @return array |
|
172
|
|
|
*/ |
|
173
|
71 |
|
public static function getPrimaryKeyVars() |
|
174
|
|
|
{ |
|
175
|
71 |
|
return !is_array(static::$primaryKey) ? [ static::$primaryKey ] : static::$primaryKey; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Get the definition for $relation |
|
180
|
|
|
* |
|
181
|
|
|
* It normalize the short definition form and create a Relation object from it. |
|
182
|
|
|
* |
|
183
|
|
|
* @param string $relation |
|
184
|
|
|
* @return Relation |
|
185
|
|
|
* @throws InvalidConfiguration |
|
186
|
|
|
* @throws UndefinedRelation |
|
187
|
|
|
*/ |
|
188
|
83 |
|
public static function getRelation($relation) |
|
189
|
|
|
{ |
|
190
|
83 |
|
if (!isset(static::$relations[$relation])) { |
|
191
|
3 |
|
throw new UndefinedRelation('Relation ' . $relation . ' is not defined'); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
82 |
|
$relDef = &static::$relations[$relation]; |
|
195
|
|
|
|
|
196
|
82 |
|
if (!$relDef instanceof Relation) { |
|
197
|
14 |
|
$relDef = Relation::createRelation($relation, $relDef); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
81 |
|
return $relDef; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Get the table name |
|
205
|
|
|
* |
|
206
|
|
|
* The table name is constructed by $tableNameTemplate and $namingSchemeTable. It can be overwritten by |
|
207
|
|
|
* $tableName. |
|
208
|
|
|
* |
|
209
|
|
|
* @return string |
|
210
|
|
|
* @throws InvalidName|InvalidConfiguration |
|
211
|
|
|
*/ |
|
212
|
154 |
|
public static function getTableName() |
|
213
|
|
|
{ |
|
214
|
154 |
|
if (static::$tableName) { |
|
215
|
11 |
|
return static::$tableName; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
143 |
|
return EM::getInstance(static::class)->getNamer() |
|
219
|
143 |
|
->getTableName(static::class, static::$tableNameTemplate, static::$namingSchemeTable); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Check if the table has a auto increment column |
|
224
|
|
|
* |
|
225
|
|
|
* @return bool |
|
226
|
|
|
*/ |
|
227
|
18 |
|
public static function isAutoIncremented() |
|
228
|
|
|
{ |
|
229
|
18 |
|
return count(static::getPrimaryKeyVars()) > 1 ? false : static::$autoIncrement; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Check if the validator is enabled |
|
234
|
|
|
* |
|
235
|
|
|
* @return bool |
|
236
|
|
|
*/ |
|
237
|
38 |
|
public static function isValidatorEnabled() |
|
238
|
|
|
{ |
|
239
|
38 |
|
return isset(self::$enabledValidators[static::class]) ? |
|
240
|
38 |
|
self::$enabledValidators[static::class] : static::$enableValidator; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Enable validator |
|
245
|
|
|
* |
|
246
|
|
|
* @param bool $enable |
|
247
|
|
|
*/ |
|
248
|
8 |
|
public static function enableValidator($enable = true) |
|
249
|
|
|
{ |
|
250
|
8 |
|
self::$enabledValidators[static::class] = $enable; |
|
251
|
8 |
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Disable validator |
|
255
|
|
|
* |
|
256
|
|
|
* @param bool $disable |
|
257
|
|
|
*/ |
|
258
|
33 |
|
public static function disableValidator($disable = true) |
|
259
|
|
|
{ |
|
260
|
33 |
|
self::$enabledValidators[static::class] = !$disable; |
|
261
|
33 |
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Validate $value for $attribute |
|
265
|
|
|
* |
|
266
|
|
|
* @param string $attribute |
|
267
|
|
|
* @param mixed $value |
|
268
|
|
|
* @return bool|Error |
|
269
|
|
|
* @throws Exception |
|
270
|
|
|
*/ |
|
271
|
10 |
|
public static function validate($attribute, $value) |
|
272
|
|
|
{ |
|
273
|
10 |
|
return static::describe()->validate(static::getColumnName($attribute), $value); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Validate $data |
|
278
|
|
|
* |
|
279
|
|
|
* $data has to be an array of $attribute => $value |
|
280
|
|
|
* |
|
281
|
|
|
* @param array $data |
|
282
|
|
|
* @return array |
|
283
|
|
|
*/ |
|
284
|
1 |
|
public static function validateArray(array $data) |
|
285
|
|
|
{ |
|
286
|
1 |
|
$result = $data; |
|
287
|
1 |
|
foreach ($result as $attribute => &$value) { |
|
288
|
1 |
|
$value = static::validate($attribute, $value); |
|
289
|
|
|
} |
|
290
|
1 |
|
return $result; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* @param EM $entityManager |
|
295
|
|
|
* @return self |
|
296
|
|
|
*/ |
|
297
|
12 |
|
public function setEntityManager(EM $entityManager) |
|
298
|
|
|
{ |
|
299
|
12 |
|
$this->entityManager = $entityManager; |
|
300
|
12 |
|
return $this; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* Get the value from $attribute |
|
305
|
|
|
* |
|
306
|
|
|
* If there is a custom getter this method get called instead. |
|
307
|
|
|
* |
|
308
|
|
|
* @param string $attribute The variable to get |
|
309
|
|
|
* @return mixed|null |
|
310
|
|
|
* @throws IncompletePrimaryKey |
|
311
|
|
|
* @throws InvalidConfiguration |
|
312
|
|
|
* @link https://tflori.github.io/orm/entities.html Working with entities |
|
313
|
|
|
*/ |
|
314
|
111 |
|
public function __get($attribute) |
|
315
|
|
|
{ |
|
316
|
111 |
|
$em = EM::getInstance(static::class); |
|
317
|
111 |
|
$getter = $em->getNamer()->getMethodName('get' . ucfirst($attribute), self::$namingSchemeMethods); |
|
318
|
|
|
|
|
319
|
111 |
|
if (method_exists($this, $getter) && is_callable([ $this, $getter ])) { |
|
320
|
4 |
|
return $this->$getter(); |
|
321
|
|
|
} else { |
|
322
|
107 |
|
$col = static::getColumnName($attribute); |
|
323
|
107 |
|
$result = isset($this->data[$col]) ? $this->data[$col] : null; |
|
324
|
|
|
|
|
325
|
107 |
|
if (!$result && isset(static::$relations[$attribute]) && isset($this->entityManager)) { |
|
326
|
1 |
|
return $this->getRelated($attribute); |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
106 |
|
return $result; |
|
330
|
|
|
} |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* Check if a column is defined |
|
335
|
|
|
* |
|
336
|
|
|
* @param $attribute |
|
337
|
|
|
* @return bool |
|
338
|
|
|
*/ |
|
339
|
3 |
|
public function __isset($attribute) |
|
340
|
|
|
{ |
|
341
|
3 |
|
$em = EM::getInstance(static::class); |
|
342
|
3 |
|
$getter = $em->getNamer()->getMethodName('get' . ucfirst($attribute), self::$namingSchemeMethods); |
|
343
|
|
|
|
|
344
|
3 |
|
if (method_exists($this, $getter) && is_callable([ $this, $getter ])) { |
|
345
|
1 |
|
return $this->$getter() !== null; |
|
346
|
|
|
} else { |
|
347
|
2 |
|
$col = static::getColumnName($attribute); |
|
348
|
2 |
|
$isset = isset($this->data[$col]); |
|
349
|
|
|
|
|
350
|
2 |
|
if (!$isset && isset(static::$relations[$attribute])) { |
|
351
|
1 |
|
return !empty($this->getRelated($attribute)); |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
1 |
|
return $isset; |
|
355
|
|
|
} |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
/** |
|
359
|
|
|
* Set $attribute to $value |
|
360
|
|
|
* |
|
361
|
|
|
* Tries to call custom setter before it stores the data directly. If there is a setter the setter needs to store |
|
362
|
|
|
* data that should be updated in the database to $data. Do not store data in $originalData as it will not be |
|
363
|
|
|
* written and give wrong results for dirty checking. |
|
364
|
|
|
* |
|
365
|
|
|
* The onChange event is called after something got changed. |
|
366
|
|
|
* |
|
367
|
|
|
* The method throws an error when the validation fails (also when the column does not exist). |
|
368
|
|
|
* |
|
369
|
|
|
* @param string $attribute The variable to change |
|
370
|
|
|
* @param mixed $value The value to store |
|
371
|
|
|
* @throws Error |
|
372
|
|
|
* @link https://tflori.github.io/orm/entities.html Working with entities |
|
373
|
|
|
*/ |
|
374
|
37 |
|
public function __set($attribute, $value) |
|
375
|
|
|
{ |
|
376
|
37 |
|
$col = $this->getColumnName($attribute); |
|
377
|
|
|
|
|
378
|
37 |
|
$em = EM::getInstance(static::class); |
|
379
|
37 |
|
$setter = $em->getNamer()->getMethodName('set' . ucfirst($attribute), self::$namingSchemeMethods); |
|
380
|
|
|
|
|
381
|
37 |
|
if (method_exists($this, $setter) && is_callable([ $this, $setter ])) { |
|
382
|
3 |
|
$oldValue = $this->__get($attribute); |
|
383
|
3 |
|
$md5OldData = md5(serialize($this->data)); |
|
384
|
3 |
|
$this->$setter($value); |
|
385
|
3 |
|
$changed = $md5OldData !== md5(serialize($this->data)); |
|
386
|
|
|
} else { |
|
387
|
34 |
|
if (static::isValidatorEnabled() && |
|
388
|
34 |
|
($error = static::validate($attribute, $value)) instanceof Error |
|
389
|
|
|
) { |
|
390
|
1 |
|
throw $error; |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
30 |
|
$oldValue = $this->__get($attribute); |
|
394
|
30 |
|
$changed = (isset($this->data[$col]) ? $this->data[$col] : null) !== $value; |
|
395
|
30 |
|
$this->data[$col] = $value; |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
33 |
|
if ($changed) { |
|
399
|
30 |
|
$this->onChange($attribute, $oldValue, $this->__get($attribute)); |
|
400
|
|
|
} |
|
401
|
33 |
|
} |
|
402
|
|
|
|
|
403
|
|
|
/** |
|
404
|
|
|
* Fill the entity with $data |
|
405
|
|
|
* |
|
406
|
|
|
* When $checkMissing is set to true it also proves that the absent columns are nullable. |
|
407
|
|
|
* |
|
408
|
|
|
* @param array $data |
|
409
|
|
|
* @param bool $ignoreUnknown |
|
410
|
|
|
* @param bool $checkMissing |
|
411
|
|
|
* @throws Error |
|
412
|
|
|
* @throws UnknownColumn |
|
413
|
|
|
*/ |
|
414
|
8 |
|
public function fill(array $data, $ignoreUnknown = false, $checkMissing = false) |
|
415
|
|
|
{ |
|
416
|
8 |
|
foreach ($data as $attribute => $value) { |
|
417
|
|
|
try { |
|
418
|
7 |
|
$this->__set($attribute, $value); |
|
419
|
2 |
|
} catch (UnknownColumn $e) { |
|
420
|
2 |
|
if (!$ignoreUnknown) { |
|
421
|
7 |
|
throw $e; |
|
422
|
|
|
} |
|
423
|
|
|
} |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
7 |
|
if ($checkMissing && is_array($errors = $this->isValid())) { |
|
427
|
1 |
|
throw $errors[0]; |
|
428
|
|
|
} |
|
429
|
6 |
|
} |
|
430
|
|
|
|
|
431
|
|
|
/** |
|
432
|
|
|
* Get related objects |
|
433
|
|
|
* |
|
434
|
|
|
* The difference between getRelated and fetch is that getRelated stores the fetched entities. To refresh set |
|
435
|
|
|
* $refresh to true. |
|
436
|
|
|
* |
|
437
|
|
|
* @param string $relation |
|
438
|
|
|
* @param bool $refresh |
|
439
|
|
|
* @return mixed |
|
440
|
|
|
* @throws Exception\NoConnection |
|
441
|
|
|
* @throws Exception\NoEntity |
|
442
|
|
|
* @throws IncompletePrimaryKey |
|
443
|
|
|
* @throws InvalidConfiguration |
|
444
|
|
|
* @throws NoEntityManager |
|
445
|
|
|
* @throws UndefinedRelation |
|
446
|
|
|
*/ |
|
447
|
11 |
|
public function getRelated($relation, $refresh = false) |
|
448
|
|
|
{ |
|
449
|
11 |
|
if ($refresh || !isset($this->relatedObjects[$relation])) { |
|
450
|
9 |
|
$this->relatedObjects[$relation] = $this->fetch($relation, true); |
|
451
|
|
|
} |
|
452
|
|
|
|
|
453
|
11 |
|
return $this->relatedObjects[$relation]; |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
|
|
/** |
|
457
|
|
|
* Set $relation to $entity |
|
458
|
|
|
* |
|
459
|
|
|
* This method is only for the owner of a relation. |
|
460
|
|
|
* |
|
461
|
|
|
* @param string $relation |
|
462
|
|
|
* @param Entity $entity |
|
463
|
|
|
* @throws IncompletePrimaryKey |
|
464
|
|
|
* @throws InvalidRelation |
|
465
|
|
|
*/ |
|
466
|
7 |
|
public function setRelated($relation, Entity $entity = null) |
|
467
|
|
|
{ |
|
468
|
7 |
|
$this::getRelation($relation)->setRelated($this, $entity); |
|
469
|
|
|
|
|
470
|
4 |
|
$this->relatedObjects[$relation] = $entity; |
|
471
|
4 |
|
} |
|
472
|
|
|
|
|
473
|
|
|
/** |
|
474
|
|
|
* Add relations for $relation to $entities |
|
475
|
|
|
* |
|
476
|
|
|
* This method is only for many-to-many relations. |
|
477
|
|
|
* |
|
478
|
|
|
* This method does not take care about already existing relations and will fail hard. |
|
479
|
|
|
* |
|
480
|
|
|
* @param string $relation |
|
481
|
|
|
* @param Entity[] $entities |
|
482
|
|
|
* @throws NoEntityManager |
|
483
|
|
|
*/ |
|
484
|
8 |
|
public function addRelated($relation, array $entities) |
|
485
|
|
|
{ |
|
486
|
|
|
// @codeCoverageIgnoreStart |
|
487
|
|
|
if (func_num_args() === 3 && func_get_arg(2) instanceof EM) { |
|
488
|
|
|
trigger_error( |
|
489
|
|
|
'Passing EntityManager to addRelated is deprecated. Use ->setEntityManager() to overwrite', |
|
490
|
|
|
E_USER_DEPRECATED |
|
491
|
|
|
); |
|
492
|
|
|
} |
|
493
|
|
|
// @codeCoverageIgnoreEnd |
|
494
|
|
|
|
|
495
|
8 |
|
$this::getRelation($relation)->addRelated($this, $entities, $this->entityManager); |
|
496
|
4 |
|
} |
|
497
|
|
|
|
|
498
|
|
|
/** |
|
499
|
|
|
* Delete relations for $relation to $entities |
|
500
|
|
|
* |
|
501
|
|
|
* This method is only for many-to-many relations. |
|
502
|
|
|
* |
|
503
|
|
|
* @param string $relation |
|
504
|
|
|
* @param Entity[] $entities |
|
505
|
|
|
* @throws NoEntityManager |
|
506
|
|
|
*/ |
|
507
|
8 |
|
public function deleteRelated($relation, $entities) |
|
508
|
|
|
{ |
|
509
|
|
|
// @codeCoverageIgnoreStart |
|
510
|
|
|
if (func_num_args() === 3 && func_get_arg(2) instanceof EM) { |
|
511
|
|
|
trigger_error( |
|
512
|
|
|
'Passing EntityManager to deleteRelated is deprecated. Use ->setEntityManager() to overwrite', |
|
513
|
|
|
E_USER_DEPRECATED |
|
514
|
|
|
); |
|
515
|
|
|
} |
|
516
|
|
|
// @codeCoverageIgnoreEnd |
|
517
|
|
|
|
|
518
|
8 |
|
$this::getRelation($relation)->deleteRelated($this, $entities, $this->entityManager); |
|
519
|
4 |
|
} |
|
520
|
|
|
|
|
521
|
|
|
/** |
|
522
|
|
|
* Resets the entity or $attribute to original data |
|
523
|
|
|
* |
|
524
|
|
|
* @param string $attribute Reset only this variable or all variables |
|
525
|
|
|
* @throws InvalidConfiguration |
|
526
|
|
|
*/ |
|
527
|
22 |
|
public function reset($attribute = null) |
|
528
|
|
|
{ |
|
529
|
22 |
|
if (!empty($attribute)) { |
|
530
|
3 |
|
$col = static::getColumnName($attribute); |
|
531
|
3 |
|
if (isset($this->originalData[$col])) { |
|
532
|
2 |
|
$this->data[$col] = $this->originalData[$col]; |
|
533
|
|
|
} else { |
|
534
|
1 |
|
unset($this->data[$col]); |
|
535
|
|
|
} |
|
536
|
3 |
|
return; |
|
537
|
|
|
} |
|
538
|
|
|
|
|
539
|
19 |
|
$this->data = $this->originalData; |
|
540
|
19 |
|
} |
|
541
|
|
|
|
|
542
|
|
|
/** |
|
543
|
|
|
* Save the entity to EntityManager |
|
544
|
|
|
* |
|
545
|
|
|
* @return Entity |
|
546
|
|
|
* @throws Exception\NoConnection |
|
547
|
|
|
* @throws Exception\NoEntity |
|
548
|
|
|
* @throws Exception\NotScalar |
|
549
|
|
|
* @throws Exception\UnsupportedDriver |
|
550
|
|
|
* @throws IncompletePrimaryKey |
|
551
|
|
|
* @throws InvalidConfiguration |
|
552
|
|
|
* @throws InvalidName |
|
553
|
|
|
* @throws NoEntityManager |
|
554
|
|
|
*/ |
|
555
|
15 |
|
public function save() |
|
556
|
|
|
{ |
|
557
|
|
|
// @codeCoverageIgnoreStart |
|
558
|
|
|
if (func_num_args() === 1 && func_get_arg(0) instanceof EM) { |
|
559
|
|
|
trigger_error( |
|
560
|
|
|
'Passing EntityManager to save is deprecated. Use ->setEntityManager() to overwrite', |
|
561
|
|
|
E_USER_DEPRECATED |
|
562
|
|
|
); |
|
563
|
|
|
} |
|
564
|
|
|
// @codeCoverageIgnoreEnd |
|
565
|
|
|
|
|
566
|
15 |
|
$inserted = false; |
|
567
|
15 |
|
$updated = false; |
|
568
|
|
|
|
|
569
|
|
|
try { |
|
570
|
|
|
// this may throw if the primary key is auto incremented but we using this to omit duplicated code |
|
571
|
15 |
|
if (!$this->entityManager->sync($this)) { |
|
572
|
2 |
|
$this->prePersist(); |
|
573
|
2 |
|
$inserted = $this->entityManager->insert($this, false); |
|
574
|
4 |
|
} elseif ($this->isDirty()) { |
|
575
|
3 |
|
$this->preUpdate(); |
|
576
|
6 |
|
$updated = $this->entityManager->update($this); |
|
577
|
|
|
} |
|
578
|
9 |
|
} catch (IncompletePrimaryKey $e) { |
|
579
|
9 |
|
if (static::isAutoIncremented()) { |
|
580
|
8 |
|
$this->prePersist(); |
|
581
|
8 |
|
$inserted = $this->entityManager->insert($this); |
|
582
|
|
|
} else { |
|
583
|
1 |
|
throw $e; |
|
584
|
|
|
} |
|
585
|
|
|
} |
|
586
|
|
|
|
|
587
|
13 |
|
$inserted && $this->postPersist(); |
|
588
|
13 |
|
$updated && $this->postUpdate(); |
|
589
|
|
|
|
|
590
|
13 |
|
return $this; |
|
591
|
|
|
} |
|
592
|
|
|
|
|
593
|
|
|
/** |
|
594
|
|
|
* Checks if entity or $attribute got changed |
|
595
|
|
|
* |
|
596
|
|
|
* @param string $attribute Check only this variable or all variables |
|
597
|
|
|
* @return bool |
|
598
|
|
|
* @throws InvalidConfiguration |
|
599
|
|
|
*/ |
|
600
|
20 |
|
public function isDirty($attribute = null) |
|
601
|
|
|
{ |
|
602
|
20 |
|
if (!empty($attribute)) { |
|
603
|
4 |
|
$col = static::getColumnName($attribute); |
|
604
|
4 |
|
return (isset($this->data[$col]) ? $this->data[$col] : null) !== |
|
605
|
4 |
|
(isset($this->originalData[$col]) ? $this->originalData[$col] : null); |
|
606
|
|
|
} |
|
607
|
|
|
|
|
608
|
17 |
|
ksort($this->data); |
|
609
|
17 |
|
ksort($this->originalData); |
|
610
|
|
|
|
|
611
|
17 |
|
return serialize($this->data) !== serialize($this->originalData); |
|
612
|
|
|
} |
|
613
|
|
|
|
|
614
|
|
|
/** |
|
615
|
|
|
* Check if the current data is valid |
|
616
|
|
|
* |
|
617
|
|
|
* Returns boolean true when valid otherwise an array of Errors. |
|
618
|
|
|
* |
|
619
|
|
|
* @return bool|Error[] |
|
620
|
|
|
*/ |
|
621
|
2 |
|
public function isValid() |
|
622
|
|
|
{ |
|
623
|
2 |
|
$result = []; |
|
624
|
|
|
|
|
625
|
2 |
|
$presentColumns = []; |
|
626
|
2 |
|
foreach ($this->data as $column => $value) { |
|
627
|
1 |
|
$presentColumns[] = $column; |
|
628
|
1 |
|
$result[] = static::validate($column, $value); |
|
629
|
|
|
} |
|
630
|
|
|
|
|
631
|
2 |
|
foreach (static::describe() as $column) { |
|
632
|
1 |
|
if (!in_array($column->name, $presentColumns)) { |
|
633
|
1 |
|
$result[] = static::validate($column->name, null); |
|
634
|
|
|
} |
|
635
|
|
|
} |
|
636
|
|
|
|
|
637
|
1 |
|
$result = array_values(array_filter($result, function ($error) { |
|
638
|
1 |
|
return $error instanceof Error; |
|
639
|
1 |
|
})); |
|
640
|
|
|
|
|
641
|
1 |
|
return count($result) === 0 ? true : $result; |
|
642
|
|
|
} |
|
643
|
|
|
|
|
644
|
|
|
/** |
|
645
|
|
|
* Empty event handler |
|
646
|
|
|
* |
|
647
|
|
|
* Get called when something is changed with magic setter. |
|
648
|
|
|
* |
|
649
|
|
|
* @param string $attribute The variable that got changed.merge(node.inheritedProperties) |
|
650
|
|
|
* @param mixed $oldValue The old value of the variable |
|
651
|
|
|
* @param mixed $value The new value of the variable |
|
652
|
|
|
* @codeCoverageIgnore dummy event handler |
|
653
|
|
|
*/ |
|
654
|
|
|
public function onChange($attribute, $oldValue, $value) |
|
|
|
|
|
|
655
|
|
|
{ |
|
656
|
|
|
} |
|
657
|
|
|
|
|
658
|
|
|
/** |
|
659
|
|
|
* Empty event handler |
|
660
|
|
|
* |
|
661
|
|
|
* Get called when the entity get initialized. |
|
662
|
|
|
* |
|
663
|
|
|
* @param bool $new Whether or not the entity is new or from database |
|
664
|
|
|
* @codeCoverageIgnore dummy event handler |
|
665
|
|
|
*/ |
|
666
|
|
|
public function onInit($new) |
|
|
|
|
|
|
667
|
|
|
{ |
|
668
|
|
|
} |
|
669
|
|
|
|
|
670
|
|
|
/** |
|
671
|
|
|
* Empty event handler |
|
672
|
|
|
* |
|
673
|
|
|
* Get called before the entity get updated in database. |
|
674
|
|
|
* |
|
675
|
|
|
* @codeCoverageIgnore dummy event handler |
|
676
|
|
|
*/ |
|
677
|
|
|
public function preUpdate() |
|
678
|
|
|
{ |
|
679
|
|
|
} |
|
680
|
|
|
|
|
681
|
|
|
/** |
|
682
|
|
|
* Empty event handler |
|
683
|
|
|
* |
|
684
|
|
|
* Get called before the entity get inserted in database. |
|
685
|
|
|
* |
|
686
|
|
|
* @codeCoverageIgnore dummy event handler |
|
687
|
|
|
*/ |
|
688
|
|
|
public function prePersist() |
|
689
|
|
|
{ |
|
690
|
|
|
} |
|
691
|
|
|
|
|
692
|
|
|
|
|
693
|
|
|
/** |
|
694
|
|
|
* Empty event handler |
|
695
|
|
|
* |
|
696
|
|
|
* Get called after the entity got inserted in database. |
|
697
|
|
|
* |
|
698
|
|
|
* @codeCoverageIgnore dummy event handler |
|
699
|
|
|
*/ |
|
700
|
|
|
public function postPersist() |
|
701
|
|
|
{ |
|
702
|
|
|
} |
|
703
|
|
|
|
|
704
|
|
|
/** |
|
705
|
|
|
* Empty event handler |
|
706
|
|
|
* |
|
707
|
|
|
* Get called after the entity got updated in database. |
|
708
|
|
|
* |
|
709
|
|
|
* @codeCoverageIgnore dummy event handler |
|
710
|
|
|
*/ |
|
711
|
|
|
public function postUpdate() |
|
712
|
|
|
{ |
|
713
|
|
|
} |
|
714
|
|
|
|
|
715
|
|
|
/** |
|
716
|
|
|
* Fetches related objects |
|
717
|
|
|
* |
|
718
|
|
|
* For relations with cardinality many it returns an EntityFetcher. Otherwise it returns the entity. |
|
719
|
|
|
* |
|
720
|
|
|
* It will throw an error for non owner when the key is incomplete. |
|
721
|
|
|
* |
|
722
|
|
|
* @param string $relation The relation to fetch |
|
723
|
|
|
* @param bool $getAll |
|
724
|
|
|
* @return Entity|Entity[]|EntityFetcher |
|
725
|
|
|
* @throws NoEntityManager |
|
726
|
|
|
*/ |
|
727
|
20 |
|
public function fetch($relation, $getAll = false) |
|
728
|
|
|
{ |
|
729
|
|
|
// @codeCoverageIgnoreStart |
|
730
|
|
|
if ($getAll instanceof EM || func_num_args() === 3 && $getAll === null) { |
|
731
|
|
|
$getAll = func_num_args() === 3 ? func_get_arg(2) : false; |
|
732
|
|
|
trigger_error( |
|
733
|
|
|
'Passing EntityManager to fetch is deprecated. Use ->setEntityManager() to overwrite', |
|
734
|
|
|
E_USER_DEPRECATED |
|
735
|
|
|
); |
|
736
|
|
|
} |
|
737
|
|
|
// @codeCoverageIgnoreEnd |
|
738
|
|
|
|
|
739
|
20 |
|
$relation = $this::getRelation($relation); |
|
740
|
|
|
|
|
741
|
20 |
|
if ($getAll) { |
|
742
|
4 |
|
return $relation->fetchAll($this, $this->entityManager); |
|
743
|
|
|
} else { |
|
744
|
16 |
|
return $relation->fetch($this, $this->entityManager); |
|
745
|
|
|
} |
|
746
|
|
|
} |
|
747
|
|
|
|
|
748
|
|
|
/** |
|
749
|
|
|
* Get the primary key |
|
750
|
|
|
* |
|
751
|
|
|
* @return array |
|
752
|
|
|
* @throws IncompletePrimaryKey |
|
753
|
|
|
*/ |
|
754
|
53 |
|
public function getPrimaryKey() |
|
755
|
|
|
{ |
|
756
|
53 |
|
$primaryKey = []; |
|
757
|
53 |
|
foreach (static::getPrimaryKeyVars() as $attribute) { |
|
758
|
53 |
|
$value = $this->$attribute; |
|
759
|
53 |
|
if ($value === null) { |
|
760
|
9 |
|
throw new IncompletePrimaryKey('Incomplete primary key - missing ' . $attribute); |
|
761
|
|
|
} |
|
762
|
49 |
|
$primaryKey[$attribute] = $value; |
|
763
|
|
|
} |
|
764
|
47 |
|
return $primaryKey; |
|
765
|
|
|
} |
|
766
|
|
|
|
|
767
|
|
|
/** |
|
768
|
|
|
* Get current data |
|
769
|
|
|
* |
|
770
|
|
|
* @return array |
|
771
|
|
|
* @internal |
|
772
|
|
|
*/ |
|
773
|
31 |
|
public function getData() |
|
774
|
|
|
{ |
|
775
|
31 |
|
return $this->data; |
|
776
|
|
|
} |
|
777
|
|
|
|
|
778
|
|
|
/** |
|
779
|
|
|
* Set new original data |
|
780
|
|
|
* |
|
781
|
|
|
* @param array $data |
|
782
|
|
|
* @internal |
|
783
|
|
|
*/ |
|
784
|
38 |
|
public function setOriginalData(array $data) |
|
785
|
|
|
{ |
|
786
|
38 |
|
$this->originalData = $data; |
|
787
|
38 |
|
} |
|
788
|
|
|
|
|
789
|
|
|
/** |
|
790
|
|
|
* String representation of data |
|
791
|
|
|
* |
|
792
|
|
|
* @link http://php.net/manual/en/serializable.serialize.php |
|
793
|
|
|
* @return string |
|
794
|
|
|
*/ |
|
795
|
2 |
|
public function serialize() |
|
796
|
|
|
{ |
|
797
|
2 |
|
return serialize([ $this->data, $this->relatedObjects ]); |
|
798
|
|
|
} |
|
799
|
|
|
|
|
800
|
|
|
/** |
|
801
|
|
|
* Constructs the object |
|
802
|
|
|
* |
|
803
|
|
|
* @link http://php.net/manual/en/serializable.unserialize.php |
|
804
|
|
|
* @param string $serialized The string representation of data |
|
805
|
|
|
*/ |
|
806
|
3 |
|
public function unserialize($serialized) |
|
807
|
|
|
{ |
|
808
|
3 |
|
list($this->data, $this->relatedObjects) = unserialize($serialized); |
|
809
|
3 |
|
$this->entityManager = EM::getInstance(static::class); |
|
810
|
3 |
|
$this->onInit(false); |
|
811
|
3 |
|
} |
|
812
|
|
|
|
|
813
|
|
|
// DEPRECATED stuff |
|
814
|
|
|
|
|
815
|
|
|
/** |
|
816
|
|
|
* @return string |
|
817
|
|
|
* @deprecated use getOption from EntityManager |
|
818
|
|
|
* @codeCoverageIgnore deprecated |
|
819
|
|
|
*/ |
|
820
|
|
|
public static function getTableNameTemplate() |
|
821
|
|
|
{ |
|
822
|
|
|
return static::$tableNameTemplate; |
|
823
|
|
|
} |
|
824
|
|
|
|
|
825
|
|
|
/** |
|
826
|
|
|
* @param string $tableNameTemplate |
|
827
|
|
|
* @deprecated use setOption from EntityManager |
|
828
|
|
|
* @codeCoverageIgnore deprecated |
|
829
|
|
|
*/ |
|
830
|
|
|
public static function setTableNameTemplate($tableNameTemplate) |
|
831
|
|
|
{ |
|
832
|
|
|
static::$tableNameTemplate = $tableNameTemplate; |
|
833
|
|
|
} |
|
834
|
|
|
|
|
835
|
|
|
/** |
|
836
|
|
|
* @return string |
|
837
|
|
|
* @deprecated use getOption from EntityManager |
|
838
|
|
|
* @codeCoverageIgnore deprecated |
|
839
|
|
|
*/ |
|
840
|
|
|
public static function getNamingSchemeTable() |
|
841
|
|
|
{ |
|
842
|
|
|
return static::$namingSchemeTable; |
|
843
|
|
|
} |
|
844
|
|
|
|
|
845
|
|
|
/** |
|
846
|
|
|
* @param string $namingSchemeTable |
|
847
|
|
|
* @deprecated use setOption from EntityManager |
|
848
|
|
|
* @codeCoverageIgnore deprecated |
|
849
|
|
|
*/ |
|
850
|
|
|
public static function setNamingSchemeTable($namingSchemeTable) |
|
851
|
|
|
{ |
|
852
|
|
|
static::$namingSchemeTable = $namingSchemeTable; |
|
853
|
|
|
} |
|
854
|
|
|
|
|
855
|
|
|
/** |
|
856
|
|
|
* @return string |
|
857
|
|
|
* @deprecated use getOption from EntityManager |
|
858
|
|
|
* @codeCoverageIgnore deprecated |
|
859
|
|
|
*/ |
|
860
|
|
|
public static function getNamingSchemeColumn() |
|
861
|
|
|
{ |
|
862
|
|
|
return static::$namingSchemeColumn; |
|
863
|
|
|
} |
|
864
|
|
|
|
|
865
|
|
|
/** |
|
866
|
|
|
* @param string $namingSchemeColumn |
|
867
|
|
|
* @deprecated use setOption from EntityManager |
|
868
|
|
|
* @codeCoverageIgnore deprecated |
|
869
|
|
|
*/ |
|
870
|
|
|
public static function setNamingSchemeColumn($namingSchemeColumn) |
|
871
|
|
|
{ |
|
872
|
|
|
static::$namingSchemeColumn = $namingSchemeColumn; |
|
873
|
|
|
} |
|
874
|
|
|
|
|
875
|
|
|
/** |
|
876
|
|
|
* @return string |
|
877
|
|
|
* @deprecated use getOption from EntityManager |
|
878
|
|
|
* @codeCoverageIgnore deprecated |
|
879
|
|
|
*/ |
|
880
|
|
|
public static function getNamingSchemeMethods() |
|
881
|
|
|
{ |
|
882
|
|
|
return static::$namingSchemeMethods; |
|
883
|
|
|
} |
|
884
|
|
|
|
|
885
|
|
|
/** |
|
886
|
|
|
* @param string $namingSchemeMethods |
|
887
|
|
|
* @deprecated use setOption from EntityManager |
|
888
|
|
|
* @codeCoverageIgnore deprecated |
|
889
|
|
|
*/ |
|
890
|
|
|
public static function setNamingSchemeMethods($namingSchemeMethods) |
|
891
|
|
|
{ |
|
892
|
|
|
static::$namingSchemeMethods = $namingSchemeMethods; |
|
893
|
|
|
} |
|
894
|
|
|
} |
|
895
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.