1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OfflineAgency\MongoAutoSync\Relationships; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Database\Eloquent\Model as EloquentModel; |
7
|
|
|
use MongoDB\BSON\ObjectID; |
8
|
|
|
|
9
|
|
|
class EmbedsOne extends EmbedsOneOrMany |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* {@inheritdoc} |
13
|
|
|
*/ |
14
|
|
|
public function initRelation(array $models, $relation) |
15
|
|
|
{ |
16
|
|
|
foreach ($models as $model) { |
17
|
|
|
$model->setRelation($relation, null); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
return $models; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
|
|
public function getResults() |
27
|
|
|
{ |
28
|
|
|
return $this->toModel($this->getEmbedded()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Save a new model and attach it to the parent model. |
33
|
|
|
* @param Model $model |
34
|
|
|
* @return Model|bool |
35
|
|
|
*/ |
36
|
|
|
public function performInsert(Model $model) |
37
|
|
|
{ |
38
|
|
|
// Generate a new key if needed. |
39
|
|
|
if ($model->getKeyName() == '_id' && ! $model->getKey()) { |
40
|
|
|
$model->setAttribute('_id', new ObjectID); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// For deeply nested documents, let the parent handle the changes. |
44
|
|
|
if ($this->isNested()) { |
45
|
|
|
$this->associate($model); |
46
|
|
|
|
47
|
|
|
return $this->parent->save() ? $model : false; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$result = $this->getBaseQuery()->update([$this->localKey => $model->getAttributes()]); |
51
|
|
|
|
52
|
|
|
// Attach the model to its parent. |
53
|
|
|
if ($result) { |
54
|
|
|
$this->associate($model); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $result ? $model : false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Save an existing model and attach it to the parent model. |
62
|
|
|
* @param Model $model |
63
|
|
|
* @return Model|bool |
64
|
|
|
*/ |
65
|
|
|
public function performUpdate(Model $model) |
66
|
|
|
{ |
67
|
|
|
if ($this->isNested()) { |
68
|
|
|
$this->associate($model); |
69
|
|
|
|
70
|
|
|
return $this->parent->save(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$values = $this->getUpdateValues($model->getDirty(), $this->localKey.'.'); |
74
|
|
|
|
75
|
|
|
$result = $this->getBaseQuery()->update($values); |
76
|
|
|
|
77
|
|
|
// Attach the model to its parent. |
78
|
|
|
if ($result) { |
79
|
|
|
$this->associate($model); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $result ? $model : false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Delete an existing model and detach it from the parent model. |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
|
|
public function performDelete() |
90
|
|
|
{ |
91
|
|
|
// For deeply nested documents, let the parent handle the changes. |
92
|
|
|
if ($this->isNested()) { |
93
|
|
|
$this->dissociate(); |
94
|
|
|
|
95
|
|
|
return $this->parent->save(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// Overwrite the local key with an empty array. |
99
|
|
|
$result = $this->getBaseQuery()->update([$this->localKey => null]); |
100
|
|
|
|
101
|
|
|
// Detach the model from its parent. |
102
|
|
|
if ($result) { |
103
|
|
|
$this->dissociate(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $result; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Attach the model to its parent. |
111
|
|
|
* @param Model $model |
112
|
|
|
* @return Model |
113
|
|
|
*/ |
114
|
|
|
public function associate(Model $model) |
115
|
|
|
{ |
116
|
|
|
return $this->setEmbedded($model->getAttributes()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Detach the model from its parent. |
121
|
|
|
* @return Model |
122
|
|
|
*/ |
123
|
|
|
public function dissociate() |
124
|
|
|
{ |
125
|
|
|
return $this->setEmbedded(null); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Delete all embedded models. |
130
|
|
|
* @return int |
131
|
|
|
*/ |
132
|
|
|
public function delete() |
133
|
|
|
{ |
134
|
|
|
return $this->performDelete(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get the name of the "where in" method for eager loading. |
139
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
140
|
|
|
* @param string $key |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
protected function whereInMethod(EloquentModel $model, $key) |
144
|
|
|
{ |
145
|
|
|
return 'whereIn'; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|