|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OfflineAgency\MongoAutoSync\Eloquent; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model as BaseModel; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use Jenssegers\Mongodb\Eloquent\Model as MongoDbModel; |
|
9
|
|
|
|
|
10
|
|
|
class Model extends MongoDbModel |
|
11
|
|
|
{ |
|
12
|
|
|
use EmbedsRelationships; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* {@inheritdoc} |
|
16
|
|
|
*/ |
|
17
|
|
|
public function setAttribute($key, $value) |
|
18
|
|
|
{ |
|
19
|
|
|
// Convert _id to ObjectID. |
|
20
|
|
|
if ($key == '_id' && is_string($value)) { |
|
21
|
|
|
$builder = $this->newBaseQueryBuilder(); |
|
22
|
|
|
|
|
23
|
|
|
$value = $builder->convertKey($value); |
|
24
|
|
|
} // Support keys in dot notation. |
|
25
|
|
|
elseif (Str::contains($key, '.')) { |
|
26
|
|
|
if (in_array($key, $this->getDates()) && $value) { |
|
27
|
|
|
$value = $this->fromDateTime($value); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
Arr::set($this->attributes, $key, $value); |
|
31
|
|
|
|
|
32
|
|
|
return; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
return BaseModel::setAttribute($key, $value); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getAttribute($key) |
|
42
|
|
|
{ |
|
43
|
|
|
// This checks for embedded relation support. |
|
44
|
|
|
if (method_exists($this, $key) && ! method_exists(self::class, $key)) { |
|
45
|
|
|
return $this->getRelationValue($key); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return BaseModel::getAttribute($key); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public function newEloquentBuilder($query) |
|
55
|
|
|
{ |
|
56
|
|
|
return new Builder($query); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|