|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by Gorlum 04.10.2017 8:23 |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace DBAL; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class ActiveRecordAbstractIndexed extends ActiveRecordAbstract { |
|
10
|
|
|
const ID_PROPERTY_NAME = 'id'; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Autoincrement index field name in DB |
|
14
|
|
|
* Would be normalized to 'id' ($id class property) |
|
15
|
|
|
* |
|
16
|
|
|
* @var string $_primaryIndexField |
|
17
|
|
|
*/ |
|
18
|
|
|
protected static $_primaryIndexField = 'id'; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param int|string $recordId |
|
24
|
|
|
* |
|
25
|
|
|
* @return string[] |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function findRecordById($recordId) { |
|
28
|
|
|
return static::findRecordFirst([self::ID_PROPERTY_NAME => $recordId]); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param int|string $recordId |
|
33
|
|
|
* |
|
34
|
|
|
* @return bool|static |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function findById($recordId) { |
|
37
|
|
|
return static::findFirst([self::ID_PROPERTY_NAME => $recordId]); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return bool |
|
42
|
|
|
*/ |
|
43
|
|
|
public function update() { |
|
44
|
|
|
if (empty($this->_changes) && empty($this->_deltas)) { |
|
45
|
|
|
return true; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$this->defaultValues(); |
|
49
|
|
|
|
|
50
|
|
|
if (!$this->dbUpdate()) { |
|
51
|
|
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
$this->acceptChanges(); |
|
56
|
|
|
// $this->_isNew = false; |
|
57
|
|
|
|
|
58
|
|
|
return true; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
// /** |
|
63
|
|
|
// * Reload current record from ID |
|
64
|
|
|
// * |
|
65
|
|
|
// * @return bool |
|
66
|
|
|
// */ |
|
67
|
|
|
// public function reload() { |
|
68
|
|
|
// //$recordId = $this->id; |
|
69
|
|
|
// $recordId = $this->{self::ID_PROPERTY_NAME}; |
|
70
|
|
|
// if (empty($recordId)) { |
|
71
|
|
|
// return false; |
|
72
|
|
|
// } |
|
73
|
|
|
// |
|
74
|
|
|
// $this->acceptChanges(); |
|
75
|
|
|
// |
|
76
|
|
|
// $fields = static::findRecordFirst($recordId); |
|
77
|
|
|
// if (empty($fields)) { |
|
78
|
|
|
// return false; |
|
79
|
|
|
// } |
|
80
|
|
|
// |
|
81
|
|
|
// $this->fromFields($fields); |
|
82
|
|
|
// $this->_isNew = false; |
|
83
|
|
|
// |
|
84
|
|
|
// return true; |
|
85
|
|
|
// } |
|
86
|
|
|
// |
|
87
|
|
|
// protected static function dbFetch(\mysqli_result $mysqliResult) { |
|
88
|
|
|
// return $mysqliResult->fetch_assoc(); |
|
89
|
|
|
// } |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return bool |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function dbUpdate() { |
|
98
|
|
|
return |
|
99
|
|
|
static::dbPrepareQuery() |
|
100
|
|
|
->setValues(empty($this->_changes) ? [] : static::translateNames($this->_changes, self::PROPERTIES_TO_FIELDS)) |
|
101
|
|
|
->setAdjust(empty($this->_deltas) ? [] : static::translateNames($this->_deltas, self::PROPERTIES_TO_FIELDS)) |
|
102
|
|
|
->setWhereArray([static::$_primaryIndexField => $this->id]) |
|
|
|
|
|
|
103
|
|
|
->doUpdate(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
// Some overrides |
|
114
|
|
|
/** |
|
115
|
|
|
* @inheritdoc |
|
116
|
|
|
*/ |
|
117
|
|
|
protected static function getFieldName($propertyName) { |
|
118
|
|
|
return |
|
119
|
|
|
$propertyName == static::ID_PROPERTY_NAME |
|
120
|
|
|
? static::$_primaryIndexField |
|
121
|
|
|
: parent::getFieldName($propertyName); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @inheritdoc |
|
126
|
|
|
*/ |
|
127
|
|
|
protected static function getPropertyName($fieldName) { |
|
128
|
|
|
return |
|
129
|
|
|
$fieldName == static::$_primaryIndexField |
|
130
|
|
|
? static::ID_PROPERTY_NAME |
|
131
|
|
|
: parent::getPropertyName($fieldName); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
// // TODO - Возможно - не нужно наследовать |
|
135
|
|
|
// public function acceptChanges() { |
|
136
|
|
|
// parent::acceptChanges(); |
|
137
|
|
|
//// $this->_isNew = empty($this->id); |
|
138
|
|
|
// } |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @inheritdoc |
|
142
|
|
|
*/ |
|
143
|
|
|
// TODO - do a check that all fields present in stored data. I.e. no empty fields with no defaults |
|
144
|
|
|
public function insert() { |
|
145
|
|
|
if(!parent::insert()) { |
|
146
|
|
|
return false; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$this->{self::ID_PROPERTY_NAME} = $this->dbLastInsertId(); |
|
150
|
|
|
$this->acceptChanges(); |
|
151
|
|
|
|
|
152
|
|
|
return true; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return int|string |
|
157
|
|
|
*/ |
|
158
|
|
|
protected function dbLastInsertId() { |
|
159
|
|
|
return static::db()->db_insert_id(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
} |
|
163
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.