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->isLoaded = true; |
55
|
|
|
|
56
|
|
|
return $cEntity; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// protected function load(KeyedContainer $cEntity) { |
|
|
|
|
60
|
|
|
// throw new \Exception('EntityModel::dbSave() is not yet implemented'); |
61
|
|
|
// } |
62
|
|
|
// |
63
|
|
|
protected function delete(KeyedContainer $cEntity) { |
64
|
|
|
$this->rowOperator->deleteById($this, $cEntity->dbId); |
65
|
|
|
$cEntity->isLoaded = false; |
66
|
|
|
$cEntity->isDeleted = true; |
67
|
|
|
throw new \Exception('KeyedModel::delete() is not yet implemented'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function insert(KeyedContainer $cEntity) { |
71
|
|
|
$cEntity->dbId = $this->rowOperator->insert($this, $this->exportRowNoId($cEntity)); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function update(KeyedContainer $cEntity) { |
75
|
|
|
// TODO - separate real changes from internal ones |
76
|
|
|
// Generate changeset row |
77
|
|
|
// Foreach all rows. If there is change and no delta - then put delta. Otherwise put change |
78
|
|
|
// If row not empty - update |
79
|
|
|
throw new \Exception('KeyedModel::update() is not yet implemented'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function unchanged(KeyedContainer $cEntity){ |
83
|
|
|
// TODO - or just save nothing ????? |
84
|
|
|
// throw new \Exception('EntityModel isNotEmpty, have dbId and not CHANGED! It can\'t be!'); |
85
|
|
|
// Do nothing |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function emptyAction(KeyedContainer $cEntity) { |
89
|
|
|
// Just created container and doesn't use it |
90
|
|
|
// throw new \Exception('EntityModel isEmpty but not loaded! It can\'t be!'); |
91
|
|
|
// Do nothing |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Gets entity's DB ID field name (which is unique within entity set) |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function getIdFieldName() { |
100
|
|
|
return $this->properties['dbId'][P_DB_FIELD]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.