1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace lroman242\LaravelCassandra\Eloquent; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Cassandra\Date; |
7
|
|
|
use Cassandra\Rows; |
8
|
|
|
use Cassandra\Time; |
9
|
|
|
use Cassandra\Timestamp; |
10
|
|
|
use lroman242\LaravelCassandra\CassandraTypesTrait; |
11
|
|
|
use lroman242\LaravelCassandra\Collection; |
12
|
|
|
use lroman242\LaravelCassandra\Query\Builder as QueryBuilder; |
13
|
|
|
use Illuminate\Database\Eloquent\Model as BaseModel; |
14
|
|
|
|
15
|
|
|
abstract class Model extends BaseModel |
16
|
|
|
{ |
17
|
|
|
use CassandraTypesTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The connection name for the model. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $connection = 'cassandra'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Indicates if the IDs are auto-incrementing. |
28
|
|
|
* This is not possible in cassandra so we override this |
29
|
|
|
* |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
public $incrementing = false; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The storage format of the model's time columns. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $timeFormat; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* List of columns in primary key |
43
|
|
|
* |
44
|
58 |
|
* @var array |
45
|
|
|
*/ |
46
|
58 |
|
protected $primaryColumns = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritdoc |
50
|
|
|
*/ |
51
|
|
|
public function newEloquentBuilder($query) |
52
|
58 |
|
{ |
53
|
|
|
return new Builder($query); |
54
|
58 |
|
} |
55
|
|
|
|
56
|
58 |
|
/** |
57
|
|
|
* @inheritdoc |
58
|
|
|
*/ |
59
|
|
|
protected function newBaseQueryBuilder() |
60
|
|
|
{ |
61
|
|
|
$connection = $this->getConnection(); |
62
|
13 |
|
|
63
|
|
|
return new QueryBuilder($connection, null, $connection->getPostProcessor()); |
|
|
|
|
64
|
13 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritdoc |
68
|
|
|
*/ |
69
|
|
|
public function freshTimestamp() |
70
|
13 |
|
{ |
71
|
|
|
return new Timestamp(); |
|
|
|
|
72
|
|
|
} |
73
|
13 |
|
|
74
|
13 |
|
/** |
75
|
|
|
* @inheritdoc |
76
|
|
|
*/ |
77
|
|
|
public function fromDateTime($value) |
78
|
1 |
|
{ |
79
|
1 |
|
// If the value is already a Timestamp instance, we don't need to parse it. |
80
|
|
|
if ($value instanceof Timestamp) { |
|
|
|
|
81
|
|
|
return $value; |
|
|
|
|
82
|
1 |
|
} |
83
|
|
|
|
84
|
|
|
// Let Eloquent convert the value to a DateTime instance. |
85
|
|
|
if (!$value instanceof \DateTime) { |
86
|
|
|
$value = parent::asDateTime($value); |
|
|
|
|
87
|
|
|
} |
88
|
8 |
|
|
89
|
|
|
return new Timestamp($value->getTimestamp() * 1000); |
|
|
|
|
90
|
|
|
} |
91
|
8 |
|
|
92
|
8 |
|
/** |
93
|
|
|
* @inheritdoc |
94
|
|
|
*/ |
95
|
|
|
protected function asDateTime($value) |
96
|
|
|
{ |
97
|
|
|
// Convert UTCDateTime instances. |
98
|
|
|
if ($value instanceof Timestamp || $value instanceof Date) { |
|
|
|
|
99
|
|
|
return Carbon::instance($value->toDateTime()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return parent::asDateTime($value); |
103
|
|
|
} |
104
|
|
|
|
105
|
8 |
|
/** |
106
|
|
|
* Get the table qualified key name. |
107
|
8 |
|
* Cassandra does not support the table.column annotation so |
108
|
|
|
* we override this |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function getQualifiedKeyName() |
113
|
|
|
{ |
114
|
|
|
return $this->getKeyName(); |
115
|
|
|
} |
116
|
2 |
|
|
117
|
|
|
/** |
118
|
2 |
|
* Qualify the given column name by the model's table. |
119
|
|
|
* |
120
|
|
|
* @param string $column |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function qualifyColumn($column) |
124
|
58 |
|
{ |
125
|
|
|
return $column; |
126
|
|
|
} |
127
|
58 |
|
|
128
|
|
|
/** |
129
|
|
|
* @inheritdoc |
130
|
|
|
*/ |
131
|
58 |
|
public function __call($method, $parameters) |
132
|
|
|
{ |
133
|
|
|
// Unset method |
134
|
|
|
if ($method == 'unset') { |
135
|
|
|
return call_user_func_array([$this, 'drop'], $parameters); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return parent::__call($method, $parameters); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Create a new Eloquent Collection instance. |
143
|
53 |
|
* |
144
|
|
|
* @param Rows|array $rows |
145
|
53 |
|
* |
146
|
|
|
* @return Collection |
147
|
|
|
* |
148
|
|
|
* @throws \Exception |
149
|
53 |
|
*/ |
150
|
53 |
|
public function newCassandraCollection($rows) |
151
|
50 |
|
{ |
152
|
|
|
if (!is_array($rows) && !$rows instanceof Rows) { |
|
|
|
|
153
|
|
|
throw new \Exception('Wrong type to create collection');//TODO: customize error |
154
|
53 |
|
} |
155
|
|
|
|
156
|
53 |
|
$items = []; |
157
|
19 |
|
foreach ($rows as $row) { |
158
|
|
|
$items[] = $this->newFromBuilder($row); |
159
|
|
|
} |
160
|
53 |
|
|
161
|
|
|
$collection = new Collection($items); |
162
|
|
|
|
163
|
|
|
if ($rows instanceof Rows) { |
|
|
|
|
164
|
|
|
$collection->setRowsInstance($rows); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $collection; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Determine if the new and old values for a given key are equivalent. |
172
|
|
|
* |
173
|
13 |
|
* @param string $key |
174
|
|
|
* @param mixed $current |
175
|
13 |
|
* |
176
|
13 |
|
* @return bool |
177
|
|
|
* |
178
|
|
|
* @throws \Exception |
179
|
2 |
|
*/ |
180
|
|
|
public function originalIsEquivalent($key, $current) |
181
|
2 |
|
{ |
182
|
2 |
|
if (!array_key_exists($key, $this->original)) { |
183
|
2 |
|
return false; |
184
|
|
|
} |
185
|
2 |
|
|
186
|
2 |
|
$original = $this->getOriginal($key); |
187
|
2 |
|
|
188
|
1 |
|
if ($current === $original) { |
189
|
|
|
return true; |
190
|
|
|
} elseif (is_null($current)) { |
191
|
1 |
|
return false; |
192
|
|
|
} elseif ($this->isDateAttribute($key)) { |
193
|
|
|
return $this->fromDateTime($current) === |
194
|
|
|
$this->fromDateTime($original); |
195
|
|
|
} elseif ($this->hasCast($key)) { |
196
|
1 |
|
return $this->castAttribute($key, $current) === |
197
|
1 |
|
$this->castAttribute($key, $original); |
198
|
|
|
} elseif ($this->isCassandraValueObject($current)) { |
199
|
|
|
return $this->valueFromCassandraObject($current) === |
200
|
|
|
$this->valueFromCassandraObject($original); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return is_numeric($current) && is_numeric($original) |
204
|
|
|
&& strcmp((string)$current, (string)$original) === 0; |
205
|
16 |
|
} |
206
|
|
|
|
207
|
16 |
|
/** |
208
|
|
|
* Get the value of the model's primary key. |
209
|
16 |
|
* |
210
|
|
|
* @return mixed |
211
|
|
|
*/ |
212
|
|
|
public function getKey() |
213
|
16 |
|
{ |
214
|
|
|
$value = $this->getAttribute($this->getKeyName()); |
215
|
|
|
|
216
|
|
|
if ($this->isCassandraValueObject($value)) { |
217
|
|
|
return $this->valueFromCassandraObject($this->getAttribute($this->getKeyName())); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return $value; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Cast an attribute to a native PHP type. |
225
|
|
|
* |
226
|
3 |
|
* @param string $key |
227
|
|
|
* @param mixed $value |
228
|
3 |
|
* |
229
|
1 |
|
* @return mixed |
230
|
1 |
|
* |
231
|
1 |
|
* @throws \Exception |
232
|
|
|
*/ |
233
|
|
|
protected function castAttribute($key, $value) |
234
|
2 |
|
{ |
235
|
|
|
if ($this->getCastType($key) == 'string' && $value instanceof Time) { |
|
|
|
|
236
|
|
|
return (new \DateTime('today', new \DateTimeZone("+0"))) |
237
|
|
|
->modify('+' . $value->seconds() . ' seconds') |
238
|
2 |
|
->format($this->getTimeFormat()); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
if ($this->getCastType($key) == 'int' && $value instanceof Time) { |
|
|
|
|
242
|
|
|
return $value->seconds(); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return parent::castAttribute($key, $value); |
246
|
1 |
|
} |
247
|
|
|
|
248
|
1 |
|
/** |
249
|
|
|
* Get the format for time stored in database. |
250
|
|
|
* |
251
|
|
|
* @return string |
252
|
|
|
*/ |
253
|
|
|
public function getTimeFormat() |
254
|
|
|
{ |
255
|
|
|
return $this->timeFormat ?: 'H:i:s'; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Get the format for time stored in database. |
260
|
|
|
* |
261
|
|
|
* @param string $format |
262
|
|
|
* |
263
|
|
|
* @return self |
264
|
|
|
*/ |
265
|
|
|
public function setTimeFormat($format) |
266
|
|
|
{ |
267
|
|
|
$this->timeFormat = $format; |
268
|
|
|
|
269
|
|
|
return $this; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Save the model to the database. |
274
|
|
|
* |
275
|
|
|
* @param array $options |
276
|
|
|
* |
277
|
|
|
* @return bool |
278
|
|
|
*/ |
279
|
|
|
public function save(array $options = []) |
280
|
|
|
{ |
281
|
|
|
$query = $this->newQueryWithoutScopes(); |
282
|
|
|
|
283
|
|
|
// If the "saving" event returns false we'll bail out of the save and return |
284
|
|
|
// false, indicating that the save failed. This provides a chance for any |
285
|
|
|
// listeners to cancel save operations if validations fail or whatever. |
286
|
|
|
if ($this->fireModelEvent('saving') === false) { |
287
|
|
|
return false; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
// If the model already exists in the database we can just update our record |
291
|
|
|
// that is already in this database using the current IDs in this "where" |
292
|
|
|
// clause to only update this model. Otherwise, we'll just insert them. |
293
|
|
|
if ($this->exists) { |
294
|
|
|
if ($this->isDirty()) { |
295
|
|
|
// If any of primary key columns where updated cassandra won't be able |
296
|
|
|
// to process update of existed record. That is why existed record will |
297
|
|
|
// be deleted and inserted new one |
298
|
|
|
$dirtyKeys = array_keys($this->getDirty()); |
299
|
|
|
$dirtyPrimaryKeys = array_intersect($this->primaryColumns, $dirtyKeys); |
300
|
|
|
$dirtyPrimaryExists = count($dirtyPrimaryKeys) > 0; |
301
|
|
|
|
302
|
|
|
// Check if any of primary key columns is dirty |
303
|
|
|
if (!$dirtyPrimaryExists) { |
304
|
|
|
$primaryColumnsExceptId = array_diff($this->primaryColumns, [$this->primaryKey]); |
305
|
|
|
|
306
|
|
|
foreach ($primaryColumnsExceptId as $key) { |
307
|
|
|
$query->where($key, $this->attributes[$key]); |
|
|
|
|
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
$saved = $this->performUpdate($query); |
|
|
|
|
311
|
|
|
} else { |
312
|
|
|
$this->fireModelEvent('updating'); |
313
|
|
|
|
314
|
|
|
// Disable model deleting, deleted, creating and created events |
315
|
|
|
$ed = $this->getEventDispatcher(); |
316
|
|
|
$this->unsetEventDispatcher(); |
317
|
|
|
|
318
|
|
|
$oldValues = $this->original; |
319
|
|
|
// Insert new record (duplicate) |
320
|
|
|
$saved = $this->performInsert($query); |
|
|
|
|
321
|
|
|
|
322
|
|
|
// Delete old record |
323
|
|
|
$deleteQuery = static::query(); |
324
|
|
|
|
325
|
|
|
foreach ($this->primaryColumns as $key) { |
326
|
|
|
$deleteQuery->where($key, $oldValues[$key]); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
$deleteQuery->delete(); |
330
|
|
|
|
331
|
|
|
//Already in silent mode |
332
|
|
|
if ($ed !== null) { |
333
|
|
|
$this->setEventDispatcher($ed); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
$this->fireModelEvent('updated'); |
337
|
|
|
} |
338
|
|
|
} else { |
339
|
|
|
$saved = true; |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
// If the model is brand new, we'll insert it into our database and set the |
344
|
|
|
// ID attribute on the model to the value of the newly inserted row's ID |
345
|
|
|
// which is typically an auto-increment value managed by the database. |
346
|
|
|
else { |
347
|
|
|
$saved = $this->performInsert($query); |
|
|
|
|
348
|
|
|
|
349
|
|
|
if (!$this->getConnectionName() && |
350
|
|
|
$connection = $query->getConnection() |
|
|
|
|
351
|
|
|
) { |
352
|
|
|
$this->setConnection($connection->getName()); |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
// If the model is successfully saved, we need to do a few more things once |
357
|
|
|
// that is done. We will call the "saved" method here to run any actions |
358
|
|
|
// we need to happen after a model gets successfully saved right here. |
359
|
|
|
if ($saved) { |
360
|
|
|
$this->finishSave($options); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
return $saved; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
} |
367
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.