|
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 save(KeyedContainer $cEntity) { |
|
64
|
|
|
if ($this->isEmpty($cEntity)) { |
|
65
|
|
|
if ($cEntity->isLoaded) { |
|
66
|
|
|
$this->rowOperator->deleteById($this, $cEntity->dbId); |
|
67
|
|
|
} else { |
|
68
|
|
|
throw new \Exception('EntityModel isEmpty but not loaded! It can\'t be!'); |
|
69
|
|
|
} |
|
70
|
|
|
} else { |
|
71
|
|
|
if (!$cEntity->dbId) { |
|
72
|
|
|
$this->rowOperator->insert($this, $this->exportRowNoId($cEntity)); |
|
|
|
|
|
|
73
|
|
|
// } elseif ($this->isChanged($cEntity)) { |
|
|
|
|
|
|
74
|
|
|
// // TODO - separate real changes from internal ones |
|
75
|
|
|
// // Generate changeset row |
|
76
|
|
|
// // Foreach all rows. If there is change and no delta - then put delta. Otherwise put change |
|
77
|
|
|
// // If row not empty - update |
|
78
|
|
|
// $this->update($cEntity); |
|
79
|
|
|
} else { |
|
80
|
|
|
// TODO - or just save nothing ????? |
|
81
|
|
|
throw new \Exception('EntityModel isNotEmpty, have dbId and not CHANGED! It can\'t be!'); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
throw new \Exception('EntityModel::save() is not yet implemented'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Gets entity's DB ID field name (which is unique within entity set) |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getIdFieldName() { |
|
94
|
|
|
return $this->properties['dbId'][P_DB_FIELD]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
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.