1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Gorlum 19.08.2016 21:26 |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Entity; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class KeyedModel |
10
|
|
|
* |
11
|
|
|
* @method KeyedContainer fromArray($array) |
12
|
|
|
* |
13
|
|
|
* @package Entity |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
class KeyedModel extends EntityModel { |
17
|
|
|
protected $newProperties = array( |
18
|
|
|
'dbId' => array( |
19
|
|
|
P_DB_FIELD => 'id', |
20
|
|
|
), |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
public function __construct(\Common\GlobalContainer $gc) { |
24
|
|
|
parent::__construct($gc); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Exports object properties to DB row state WITHOUT ID |
29
|
|
|
* |
30
|
|
|
* Useful for INSERT operations |
31
|
|
|
* |
32
|
|
|
* @param \Entity\KeyedContainer $cEntity |
33
|
|
|
*/ |
34
|
|
|
protected function exportRowNoId($cEntity) { |
35
|
|
|
$this->exportRow($cEntity); |
36
|
|
|
|
37
|
|
|
if (($idFieldName = $this->getIdFieldName()) != '') { |
38
|
|
|
unset($cEntity->row[$idFieldName]); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param int|string $dbId |
44
|
|
|
* |
45
|
|
|
* @return KeyedContainer|false |
46
|
|
|
*/ |
47
|
|
|
public function loadById($dbId) { |
48
|
|
|
$row = $this->rowOperator->getById($this, $dbId); |
49
|
|
|
if (empty($row)) { |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$cEntity = $this->fromArray($row); |
54
|
|
|
$cEntity->dbStatus = DB_RECORD_LOADED; |
55
|
|
|
|
56
|
|
|
return $cEntity; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param KeyedContainer $cEntity |
61
|
|
|
* |
62
|
|
|
* @throws \Exception |
63
|
|
|
*/ |
64
|
|
|
protected function delete($cEntity) { |
65
|
|
|
$this->rowOperator->deleteById($this, $cEntity->dbId); |
66
|
|
|
$cEntity->dbStatus = DB_RECORD_DELETED; |
67
|
|
|
throw new \Exception('KeyedModel::delete() is not yet implemented'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param KeyedContainer $cEntity |
72
|
|
|
*/ |
73
|
|
|
protected function insert($cEntity) { |
74
|
|
|
$cEntity->dbId = $this->rowOperator->insert($this, $this->exportRowNoId($cEntity)); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param KeyedContainer $cEntity |
79
|
|
|
* |
80
|
|
|
* @throws \Exception |
81
|
|
|
*/ |
82
|
|
|
protected function update($cEntity) { |
83
|
|
|
// TODO - separate real changes from internal ones |
84
|
|
|
// Generate changeset row |
85
|
|
|
// Foreach all rows. If there is change and no delta - then put delta. Otherwise put change |
86
|
|
|
// If row not empty - update |
87
|
|
|
throw new \Exception('KeyedModel::update() is not yet implemented'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param KeyedContainer $cEntity |
92
|
|
|
*/ |
93
|
|
|
protected function onSaveUnchanged($cEntity){ |
94
|
|
|
// TODO - or just save nothing ????? |
95
|
|
|
// throw new \Exception('EntityModel isNotEmpty, have dbId and not CHANGED! It can\'t be!'); |
96
|
|
|
// Do nothing |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param KeyedContainer $cEntity |
101
|
|
|
*/ |
102
|
|
|
protected function onSaveNew($cEntity) { |
103
|
|
|
// Just created container and doesn't use it |
104
|
|
|
// throw new \Exception('EntityModel isEmpty but not loaded! It can\'t be!'); |
105
|
|
|
// Do nothing |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Gets entity's DB ID field name (which is unique within entity set) |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function getIdFieldName() { |
114
|
|
|
return $this->properties['dbId'][P_DB_FIELD]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: