1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BaoPham\DynamoDb; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use DateTime; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class DynamoDbModel. |
11
|
|
|
*/ |
12
|
|
|
abstract class DynamoDbModel extends Model |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Always set this to false since DynamoDb does not support incremental Id. |
16
|
|
|
* |
17
|
|
|
* @var bool |
18
|
|
|
*/ |
19
|
|
|
public $incrementing = false; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \BaoPham\DynamoDb\DynamoDbClientInterface |
23
|
|
|
*/ |
24
|
|
|
protected static $dynamoDb; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @deprecated |
28
|
|
|
* @var \Aws\DynamoDb\Marshaler |
29
|
|
|
*/ |
30
|
|
|
protected $marshaler; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @deprecated |
34
|
|
|
* @var \BaoPham\DynamoDb\EmptyAttributeFilter |
35
|
|
|
*/ |
36
|
|
|
protected $attributeFilter; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Indexes. |
40
|
|
|
* [ |
41
|
|
|
* '<simple_index_name>' => [ |
42
|
|
|
* 'hash' => '<index_key>' |
43
|
|
|
* ], |
44
|
|
|
* '<composite_index_name>' => [ |
45
|
|
|
* 'hash' => '<index_hash_key>', |
46
|
|
|
* 'range' => '<index_range_key>' |
47
|
|
|
* ], |
48
|
|
|
* ] |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $dynamoDbIndexKeys = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Array of your composite key. |
56
|
|
|
* ['<hash>', '<range>'] |
57
|
|
|
* |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
protected $compositeKey = []; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Default Date format |
64
|
|
|
* ISO 8601 Compliant |
65
|
|
|
*/ |
66
|
|
|
protected $dateFormat = DateTime::ATOM; |
67
|
|
|
|
68
|
|
|
|
69
|
115 |
|
public function __construct(array $attributes = []) |
70
|
|
|
{ |
71
|
115 |
|
$this->bootIfNotBooted(); |
72
|
|
|
|
73
|
115 |
|
$this->syncOriginal(); |
74
|
|
|
|
75
|
115 |
|
$this->fill($attributes); |
76
|
|
|
|
77
|
115 |
|
$this->setupDynamoDb(); |
78
|
115 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the DynamoDbClient service that is being used by the models. |
82
|
|
|
* |
83
|
|
|
* @return DynamoDbClientInterface |
84
|
|
|
*/ |
85
|
3 |
|
public static function getDynamoDbClientService() |
86
|
|
|
{ |
87
|
3 |
|
return static::$dynamoDb; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set the DynamoDbClient used by models. |
92
|
|
|
* |
93
|
|
|
* @param DynamoDbClientInterface $dynamoDb |
94
|
|
|
* |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
130 |
|
public static function setDynamoDbClientService(DynamoDbClientInterface $dynamoDb) |
98
|
|
|
{ |
99
|
130 |
|
static::$dynamoDb = $dynamoDb; |
100
|
130 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Unset the DynamoDbClient service for models. |
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
3 |
|
public static function unsetDynamoDbClientService() |
108
|
|
|
{ |
109
|
3 |
|
static::$dynamoDb = null; |
110
|
3 |
|
} |
111
|
|
|
|
112
|
115 |
|
protected function setupDynamoDb() |
113
|
|
|
{ |
114
|
115 |
|
$this->marshaler = static::$dynamoDb->getMarshaler(); |
115
|
115 |
|
$this->attributeFilter = static::$dynamoDb->getAttributeFilter(); |
116
|
115 |
|
} |
117
|
|
|
|
118
|
8 |
|
public function save(array $options = []) |
119
|
|
|
{ |
120
|
8 |
|
$create = !$this->exists; |
121
|
|
|
|
122
|
8 |
|
if ($this->fireModelEvent('saving') === false) { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
8 |
|
if ($create && $this->fireModelEvent('creating') === false) { |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
8 |
|
if (!$create && $this->fireModelEvent('updating') === false) { |
131
|
|
|
return false; |
132
|
|
|
} |
133
|
|
|
|
134
|
8 |
|
if ($this->usesTimestamps()) { |
135
|
8 |
|
$this->updateTimestamps(); |
136
|
|
|
} |
137
|
|
|
|
138
|
8 |
|
$saved = $this->newQuery()->save(); |
139
|
|
|
|
140
|
8 |
|
if (!$saved) { |
141
|
|
|
return $saved; |
142
|
|
|
} |
143
|
|
|
|
144
|
8 |
|
$this->exists = true; |
145
|
8 |
|
$this->wasRecentlyCreated = $create; |
146
|
8 |
|
$this->fireModelEvent($create ? 'created' : 'updated', false); |
147
|
|
|
|
148
|
8 |
|
$this->finishSave($options); |
149
|
|
|
|
150
|
8 |
|
return $saved; |
151
|
|
|
} |
152
|
|
|
|
153
|
3 |
|
public function update(array $attributes = [], array $options = []) |
154
|
|
|
{ |
155
|
3 |
|
return $this->fill($attributes)->save(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public static function create(array $attributes = []) |
159
|
|
|
{ |
160
|
|
|
$model = new static; |
161
|
|
|
|
162
|
|
|
$model->fill($attributes)->save(); |
163
|
|
|
|
164
|
|
|
return $model; |
165
|
|
|
} |
166
|
|
|
|
167
|
2 |
|
public function delete() |
168
|
|
|
{ |
169
|
2 |
|
if (is_null($this->getKeyName())) { |
170
|
|
|
throw new Exception('No primary key defined on model.'); |
171
|
|
|
} |
172
|
|
|
|
173
|
2 |
|
if ($this->exists) { |
174
|
2 |
|
if ($this->fireModelEvent('deleting') === false) { |
175
|
|
|
return false; |
176
|
|
|
} |
177
|
|
|
|
178
|
2 |
|
$this->exists = false; |
179
|
|
|
|
180
|
2 |
|
$success = $this->newQuery()->delete(); |
181
|
|
|
|
182
|
2 |
|
if ($success) { |
183
|
2 |
|
$this->fireModelEvent('deleted', false); |
184
|
|
|
} |
185
|
|
|
|
186
|
2 |
|
return $success; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
7 |
|
public static function all($columns = []) |
191
|
|
|
{ |
192
|
7 |
|
$instance = new static; |
193
|
|
|
|
194
|
7 |
|
return $instance->newQuery()->get($columns); |
195
|
|
|
} |
196
|
|
|
|
197
|
2 |
|
public function refresh() |
198
|
|
|
{ |
199
|
2 |
|
if (! $this->exists) { |
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
2 |
|
$query = $this->newQuery(); |
204
|
|
|
|
205
|
2 |
|
$refreshed = $query->find($this->getKeys()); |
206
|
|
|
|
207
|
2 |
|
$this->setRawAttributes($refreshed->toArray()); |
208
|
|
|
|
209
|
2 |
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return DynamoDbQueryBuilder |
214
|
|
|
*/ |
215
|
115 |
|
public function newQuery() |
216
|
|
|
{ |
217
|
115 |
|
$builder = new DynamoDbQueryBuilder($this); |
218
|
|
|
|
219
|
115 |
|
foreach ($this->getGlobalScopes() as $identifier => $scope) { |
220
|
7 |
|
$builder->withGlobalScope($identifier, $scope); |
221
|
|
|
} |
222
|
|
|
|
223
|
115 |
|
return $builder; |
224
|
|
|
} |
225
|
|
|
|
226
|
100 |
|
public function hasCompositeKey() |
227
|
|
|
{ |
228
|
100 |
|
return !empty($this->compositeKey); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @deprecated |
233
|
|
|
* @param $item |
234
|
|
|
* @return array |
235
|
|
|
*/ |
236
|
|
|
public function marshalItem($item) |
237
|
|
|
{ |
238
|
|
|
return $this->marshaler->marshalItem($item); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @deprecated |
243
|
|
|
* @param $value |
244
|
|
|
* @return array |
245
|
|
|
*/ |
246
|
|
|
public function marshalValue($value) |
247
|
|
|
{ |
248
|
|
|
return $this->marshaler->marshalValue($value); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @deprecated |
253
|
|
|
* @param $item |
254
|
|
|
* @return array|\stdClass |
255
|
|
|
*/ |
256
|
|
|
public function unmarshalItem($item) |
257
|
|
|
{ |
258
|
|
|
return $this->marshaler->unmarshalItem($item); |
259
|
|
|
} |
260
|
|
|
|
261
|
30 |
|
public function setId($id) |
262
|
|
|
{ |
263
|
30 |
|
if (!is_array($id)) { |
264
|
11 |
|
$this->setAttribute($this->getKeyName(), $id); |
265
|
|
|
|
266
|
11 |
|
return $this; |
267
|
|
|
} |
268
|
|
|
|
269
|
21 |
|
foreach ($id as $keyName => $value) { |
270
|
21 |
|
$this->setAttribute($keyName, $value); |
271
|
|
|
} |
272
|
|
|
|
273
|
21 |
|
return $this; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @return \Aws\DynamoDb\DynamoDbClient |
278
|
|
|
*/ |
279
|
115 |
|
public function getClient() |
280
|
|
|
{ |
281
|
115 |
|
return static::$dynamoDb->getClient($this->connection); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Get the value of the model's primary key. |
286
|
|
|
* |
287
|
|
|
* @return mixed |
288
|
|
|
*/ |
289
|
|
|
public function getKey() |
290
|
|
|
{ |
291
|
|
|
return $this->getAttribute($this->getKeyName()); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Get the value of the model's primary / composite key. |
296
|
|
|
* Use this if you always want the key values in associative array form. |
297
|
|
|
* |
298
|
|
|
* @return array |
299
|
|
|
* |
300
|
|
|
* ['id' => 'foo'] |
301
|
|
|
* |
302
|
|
|
* or |
303
|
|
|
* |
304
|
|
|
* ['id' => 'foo', 'id2' => 'bar'] |
305
|
|
|
*/ |
306
|
36 |
|
public function getKeys() |
307
|
|
|
{ |
308
|
36 |
|
if ($this->hasCompositeKey()) { |
309
|
17 |
|
$key = []; |
310
|
|
|
|
311
|
17 |
|
foreach ($this->compositeKey as $name) { |
312
|
17 |
|
$key[$name] = $this->getAttribute($name); |
313
|
|
|
} |
314
|
|
|
|
315
|
17 |
|
return $key; |
316
|
|
|
} |
317
|
|
|
|
318
|
19 |
|
$name = $this->getKeyName(); |
319
|
|
|
|
320
|
19 |
|
return [$name => $this->getAttribute($name)]; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Get the primary key for the model. |
325
|
|
|
* |
326
|
|
|
* @return string |
327
|
|
|
*/ |
328
|
22 |
|
public function getKeyName() |
329
|
|
|
{ |
330
|
22 |
|
return $this->primaryKey; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Get the primary/composite key for the model. |
335
|
|
|
* |
336
|
|
|
* @return array |
337
|
|
|
*/ |
338
|
96 |
|
public function getKeyNames() |
339
|
|
|
{ |
340
|
96 |
|
return $this->hasCompositeKey() ? $this->compositeKey : [$this->primaryKey]; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* @return array |
345
|
|
|
*/ |
346
|
68 |
|
public function getDynamoDbIndexKeys() |
347
|
|
|
{ |
348
|
68 |
|
return $this->dynamoDbIndexKeys; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @param array $dynamoDbIndexKeys |
353
|
|
|
*/ |
354
|
|
|
public function setDynamoDbIndexKeys($dynamoDbIndexKeys) |
355
|
|
|
{ |
356
|
|
|
$this->dynamoDbIndexKeys = $dynamoDbIndexKeys; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* @deprecated |
361
|
|
|
* @return \Aws\DynamoDb\Marshaler |
362
|
|
|
*/ |
363
|
|
|
public function getMarshaler() |
364
|
|
|
{ |
365
|
|
|
return $this->marshaler; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Remove non-serializable properties when serializing. |
370
|
|
|
* |
371
|
|
|
* @return array |
372
|
|
|
*/ |
373
|
2 |
|
public function __sleep() |
374
|
|
|
{ |
375
|
2 |
|
return array_keys( |
376
|
2 |
|
array_except(get_object_vars($this), ['marshaler', 'attributeFilter']) |
377
|
|
|
); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* When a model is being unserialized, check if it needs to be booted and setup DynamoDB. |
382
|
|
|
* |
383
|
|
|
* @return void |
384
|
|
|
*/ |
385
|
2 |
|
public function __wakeup() |
386
|
|
|
{ |
387
|
2 |
|
parent::__wakeup(); |
388
|
2 |
|
$this->setupDynamoDb(); |
389
|
2 |
|
} |
390
|
|
|
} |
391
|
|
|
|