-
Notifications
You must be signed in to change notification settings - Fork 1
/
ActiveRecord.php
257 lines (219 loc) · 5.69 KB
/
ActiveRecord.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ICanBoogie;
use ICanBoogie\ActiveRecord\Model;
/**
* Active Record facilitates the creation and use of business objects whose data require persistent
* storage via database.
*
* @property-read Model $model The model managing the active record.
* @property-read string $model_id The identifier of the model managing the active record.
*/
class ActiveRecord extends Prototyped
{
/**
* The identifier of the model managing the record.
*
* @var string
*/
const MODEL_ID = null;
/**
* Model managing the active record.
*
* @var Model
*/
private $model;
/**
* Identifier of the model managing the active record.
*
* Note: Due to a PHP bug (or feature), the visibility of the property MUST NOT be private.
* https://bugs.php.net/bug.php?id=40412
*
* @var string
*/
protected $model_id;
/**
* Initializes the {@link $model} and {@link $model_id} properties.
*
* @param string|Model|null $model The model managing the active record. A {@link Model}
* instance can be specified as well as a model identifier. If a model identifier is
* specified, the model is resolved when the {@link $model} property is accessed. If `$model`
* is empty, the identifier of the model is read from the {@link MODEL_ID} class constant.
*
* @throws \InvalidArgumentException if $model is neither a model identifier nor a
* {@link Model} instance.
*/
public function __construct($model = null)
{
if (!$model)
{
$model = static::MODEL_ID;
}
if (is_string($model))
{
$this->model_id = $model;
}
else if ($model instanceof Model)
{
$this->model = $model;
$this->model_id = $model->id;
}
else
{
throw new \InvalidArgumentException("\$model must be an instance of ICanBoogie\\ActiveRecord\\Model or a model identifier. Given:" . (is_object($model) ? get_class($model) : gettype($model)));
}
}
/**
* Removes the {@link $model} property.
*
* Properties whose value are instances of the {@link ActiveRecord} class are removed from the
* exported properties.
*/
public function __sleep(): array
{
$properties = parent::__sleep();
unset($properties['model']);
foreach (array_keys($properties) as $property)
{
if ($this->$property instanceof self)
{
unset($properties[$property]);
}
}
return $properties;
}
/**
* Removes `model` from the output, since `model_id` is good enough to figure which model
* is used.
*
* @return array
*/
public function __debugInfo(): array
{
$array = (array) $this;
unset($array["\0" . __CLASS__ . "\0model"]);
return $array;
}
/**
* Returns the model managing the active record.
*
* @return Model
*/
protected function get_model(): Model
{
return $this->model
?: $this->model = ActiveRecord\get_model($this->model_id);
}
/**
* Returns the identifier of the model managing the active record.
*
* @return string
*/
protected function get_model_id()
{
return $this->model_id;
}
/**
* Saves the active record using its model.
*
* @return int|bool Primary key value of the active record, or a boolean if the primary key
* is not a serial.
*/
public function save()
{
$model = $this->get_model();
$primary = $model->primary;
$properties = $this->alter_persistent_properties($this->to_array(), $model);
#
# Multipart primary key
#
if (is_array($primary))
{
return $model->insert($properties, [ 'on duplicate' => true ]);
}
#
# Non auto-increment primary key, unless the the key is inherit from parent model.
#
if (!$model->parent && $primary && isset($properties[$primary])
&& !$model->extended_schema[$primary]->auto_increment)
{
return $model->insert($properties, [ 'on duplicate' => true ]);
}
#
# Auto-increment primary key
#
$key = null;
if (isset($properties[$primary]))
{
$key = $properties[$primary];
unset($properties[$primary]);
}
$rc = $model->save($properties, $key);
if ($key === null && $rc)
{
$this->update_primary_key($rc);
}
return $rc;
}
/**
* Unless it's an acceptable value for a column, columns with `null` values are discarded.
* This way, we don't have to define every properties before saving our active record.
*
* @param array $properties
* @param Model $model
*
* @return array The altered persistent properties
*/
protected function alter_persistent_properties(array $properties, Model $model): array
{
$schema = $model->extended_schema;
foreach ($properties as $identifier => $value)
{
if ($value !== null || (isset($schema[$identifier]) && $schema[$identifier]->null))
{
continue;
}
unset($properties[$identifier]);
}
return $properties;
}
/**
* Updates primary key.
*
* @param array|string|int $primary_key
*/
protected function update_primary_key($primary_key)
{
$model = $this->model;
$property = $model->primary;
if (!$property)
{
throw new \LogicException("Unable to update primary key, model `$model->id` doesn't define one.");
}
$this->$property = $primary_key;
}
/**
* Deletes the active record using its model.
*
* @return bool `true` if the record was deleted, `false` otherwise.
*
* @throws \Exception in attempt to delete a record from a model which primary key is empty.
*/
public function delete(): bool
{
$model = $this->model;
$primary = $model->primary;
if (!$primary)
{
throw new \LogicException("Unable to delete record, model `$model->id` doesn't have a primary key.");
}
return $model->delete($this->$primary);
}
}