|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OfflineAgency\MongoAutoSync\Eloquent; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
|
6
|
|
|
use Jenssegers\Mongodb\Eloquent\Builder as MongoDbEloquentBuilder; |
|
7
|
|
|
|
|
8
|
|
|
class Builder extends MongoDbEloquentBuilder |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* {@inheritdoc} |
|
12
|
|
|
*/ |
|
13
|
|
|
public function update(array $values, array $options = []) |
|
14
|
|
|
{ |
|
15
|
|
|
// Intercept operations on embedded models and delegate logic |
|
16
|
|
|
// to the parent relation instance. |
|
17
|
|
|
if ($relation = $this->model->getParentRelation()) { |
|
18
|
|
|
$relation->performUpdate($this->model, $values); |
|
19
|
|
|
|
|
20
|
|
|
return 1; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
return $this->toBase()->update($this->addUpdatedAtColumn($values), $options); |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
|
|
public function insert(array $values) |
|
30
|
|
|
{ |
|
31
|
|
|
// Intercept operations on embedded models and delegate logic |
|
32
|
|
|
// to the parent relation instance. |
|
33
|
|
|
if ($relation = $this->model->getParentRelation()) { |
|
34
|
|
|
$relation->performInsert($this->model, $values); |
|
35
|
|
|
|
|
36
|
|
|
return true; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return EloquentBuilder::insert($values); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
public function insertGetId(array $values, $sequence = null) |
|
46
|
|
|
{ |
|
47
|
|
|
// Intercept operations on embedded models and delegate logic |
|
48
|
|
|
// to the parent relation instance. |
|
49
|
|
|
if ($relation = $this->model->getParentRelation()) { |
|
50
|
|
|
$relation->performInsert($this->model, $values); |
|
51
|
|
|
|
|
52
|
|
|
return $this->model->getKey(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return EloquentBuilder::insertGetId($values, $sequence); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public function delete() |
|
62
|
|
|
{ |
|
63
|
|
|
// Intercept operations on embedded models and delegate logic |
|
64
|
|
|
// to the parent relation instance. |
|
65
|
|
|
if ($relation = $this->model->getParentRelation()) { |
|
66
|
|
|
$relation->performDelete($this->model); |
|
67
|
|
|
|
|
68
|
|
|
return $this->model->getKey(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return EloquentBuilder::delete(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
|
|
public function increment($column, $amount = 1, array $extra = []) |
|
78
|
|
|
{ |
|
79
|
|
|
// Intercept operations on embedded models and delegate logic |
|
80
|
|
|
// to the parent relation instance. |
|
81
|
|
|
if ($relation = $this->model->getParentRelation()) { |
|
|
|
|
|
|
82
|
|
|
$value = $this->model->{$column}; |
|
83
|
|
|
|
|
84
|
|
|
// When doing increment and decrements, Eloquent will automatically |
|
85
|
|
|
// sync the original attributes. We need to change the attribute |
|
86
|
|
|
// temporary in order to trigger an update query. |
|
87
|
|
|
$this->model->{$column} = null; |
|
88
|
|
|
|
|
89
|
|
|
$this->model->syncOriginalAttribute($column); |
|
90
|
|
|
|
|
91
|
|
|
$result = $this->model->update([$column => $value]); |
|
92
|
|
|
|
|
93
|
|
|
return $result; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return EloquentBuilder::increment($column, $amount, $extra); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public function decrement($column, $amount = 1, array $extra = []) |
|
103
|
|
|
{ |
|
104
|
|
|
// Intercept operations on embedded models and delegate logic |
|
105
|
|
|
// to the parent relation instance. |
|
106
|
|
|
if ($relation = $this->model->getParentRelation()) { |
|
|
|
|
|
|
107
|
|
|
$value = $this->model->{$column}; |
|
108
|
|
|
|
|
109
|
|
|
// When doing increment and decrements, Eloquent will automatically |
|
110
|
|
|
// sync the original attributes. We need to change the attribute |
|
111
|
|
|
// temporary in order to trigger an update query. |
|
112
|
|
|
$this->model->{$column} = null; |
|
113
|
|
|
|
|
114
|
|
|
$this->model->syncOriginalAttribute($column); |
|
115
|
|
|
|
|
116
|
|
|
return $this->model->update([$column => $value]); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return EloquentBuilder::decrement($column, $amount, $extra); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* {@inheritdoc} |
|
124
|
|
|
*/ |
|
125
|
|
|
public function chunkById($count, callable $callback, $column = '_id', $alias = null) |
|
126
|
|
|
{ |
|
127
|
|
|
return EloquentBuilder::chunkById($count, $callback, $column, $alias); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritdoc} |
|
132
|
|
|
*/ |
|
133
|
|
|
public function raw($expression = null) |
|
134
|
|
|
{ |
|
135
|
|
|
// Get raw results from the query builder. |
|
136
|
|
|
$results = $this->query->raw($expression); |
|
137
|
|
|
|
|
138
|
|
|
// Convert MongoCursor results to a collection of models. |
|
139
|
|
|
if ($results instanceof Cursor) { |
|
|
|
|
|
|
140
|
|
|
$results = iterator_to_array($results, false); |
|
141
|
|
|
|
|
142
|
|
|
return $this->model->hydrate($results); |
|
143
|
|
|
} // Convert Mongo BSONDocument to a single object. |
|
144
|
|
|
elseif ($results instanceof BSONDocument) { |
|
|
|
|
|
|
145
|
|
|
$results = $results->getArrayCopy(); |
|
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
return $this->model->newFromBuilder((array) $results); |
|
148
|
|
|
} // The result is a single object. |
|
149
|
|
|
elseif (is_array($results) && array_key_exists('_id', $results)) { |
|
|
|
|
|
|
150
|
|
|
return $this->model->newFromBuilder((array) $results); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return $results; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Add the "updated at" column to an array of values. |
|
158
|
|
|
* TODO Remove if https://github.com/laravel/framework/commit/6484744326531829341e1ff886cc9b628b20d73e |
|
159
|
|
|
* wiil be reverted |
|
160
|
|
|
* Issue in laravel frawework https://github.com/laravel/framework/issues/27791. |
|
161
|
|
|
* @param array $values |
|
162
|
|
|
* @return array |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function addUpdatedAtColumn(array $values) |
|
165
|
|
|
{ |
|
166
|
|
|
if (! $this->model->usesTimestamps() || $this->model->getUpdatedAtColumn() === null) { |
|
167
|
|
|
return $values; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
$column = $this->model->getUpdatedAtColumn(); |
|
171
|
|
|
$values = array_merge( |
|
172
|
|
|
[$column => $this->model->freshTimestampString()], |
|
173
|
|
|
$values |
|
174
|
|
|
); |
|
175
|
|
|
|
|
176
|
|
|
return $values; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.