forked from octobercms/library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMorphOneOrMany.php
More file actions
321 lines (283 loc) · 10.4 KB
/
MorphOneOrMany.php
File metadata and controls
321 lines (283 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php namespace October\Rain\Database\Relations;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
/**
* MorphOneOrMany
*
* @package october\database
* @author Alexey Bobkov, Samuel Georges
*/
trait MorphOneOrMany
{
use DeferOneOrMany;
/**
* @var string relationName is the "name" of the relationship.
*/
protected $relationName;
/**
* save the supplied related model with deferred binding support.
*/
public function save(Model $model, $sessionKey = null)
{
if ($sessionKey === null) {
return parent::save($model);
}
$this->add($model, $sessionKey);
return $model->save() ? $model : false;
}
/**
* saveQuietly saves the supplied related model without raising any events,
* with deferred binding support.
*/
public function saveQuietly(Model $model, $sessionKey = null)
{
return Model::withoutEvents(function () use ($model, $sessionKey) {
return $this->save($model, $sessionKey);
});
}
/**
* saveMany saves multiple models with deferred binding support.
*/
public function saveMany($models, $sessionKey = null)
{
foreach ($models as $model) {
$this->save($model, $sessionKey);
}
return $models;
}
/**
* saveManyQuietly saves multiple models without raising any events,
* with deferred binding support.
*/
public function saveManyQuietly($models, $sessionKey = null)
{
return Model::withoutEvents(function () use ($models, $sessionKey) {
return $this->saveMany($models, $sessionKey);
});
}
/**
* create a new instance of this related model with deferred binding support.
*/
public function create(array $attributes = [], $sessionKey = null)
{
$model = parent::create($attributes);
if ($sessionKey !== null) {
$this->add($model, $sessionKey);
}
return $model;
}
/**
* createQuietly creates a new instance without raising any events,
* with deferred binding support.
*/
public function createQuietly(array $attributes = [], $sessionKey = null)
{
return Model::withoutEvents(function () use ($attributes, $sessionKey) {
return $this->create($attributes, $sessionKey);
});
}
/**
* forceCreateQuietly creates a new instance bypassing mass assignment
* without raising any events, with deferred binding support.
*/
public function forceCreateQuietly(array $attributes = [], $sessionKey = null)
{
return Model::withoutEvents(function () use ($attributes, $sessionKey) {
$model = parent::forceCreate($attributes);
if ($sessionKey !== null) {
$this->add($model, $sessionKey);
}
return $model;
});
}
/**
* createMany creates multiple related models with deferred binding support.
*/
public function createMany(iterable $records, $sessionKey = null)
{
$instances = parent::createMany($records);
if ($sessionKey !== null) {
foreach ($instances as $model) {
$this->add($model, $sessionKey);
}
}
return $instances;
}
/**
* createManyQuietly creates multiple models without raising any events,
* with deferred binding support.
*/
public function createManyQuietly(iterable $records, $sessionKey = null)
{
return Model::withoutEvents(function () use ($records, $sessionKey) {
return $this->createMany($records, $sessionKey);
});
}
/**
* createOrFirst attempts to create the record, or if a unique constraint
* violation occurs, finds the existing record.
*/
public function createOrFirst(array $attributes = [], array $values = [], $sessionKey = null)
{
$model = parent::createOrFirst($attributes, $values);
if ($sessionKey !== null) {
$this->add($model, $sessionKey);
}
return $model;
}
/**
* add a model to this relationship type.
*/
public function add(Model $model, $sessionKey = null)
{
if ($sessionKey === null) {
/**
* @event model.relation.beforeAdd
* Called before adding a relation to the model (for AttachOneOrMany, HasOneOrMany & MorphOneOrMany relations)
*
* Example usage:
*
* $model->bindEvent('model.relation.beforeAdd', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) {
* if ($relationName === 'some_relation') {
* return false;
* }
* });
*
*/
if ($this->parent->fireEvent('model.relation.beforeAdd', [$this->relationName, $model], true) === false) {
return;
}
// Associate the model
if ($this->parent->exists) {
$model->setAttribute($this->getForeignKeyName(), $this->getParentKey());
$model->setAttribute($this->getMorphType(), $this->morphClass);
$model->save();
}
else {
$this->parent->bindEventOnce('model.afterSave', function () use ($model) {
$model->setAttribute($this->getForeignKeyName(), $this->getParentKey());
$model->setAttribute($this->getMorphType(), $this->morphClass);
$model->save();
});
}
// Use the opportunity to set the relation in memory
if ($this instanceof MorphOne) {
$this->parent->setRelation($this->relationName, $model);
}
else {
$this->parent->unsetRelation($this->relationName);
}
/**
* @event model.relation.add
* Called after adding a relation to the model (for AttachOneOrMany, HasOneOrMany & MorphOneOrMany relations)
*
* Example usage:
*
* $model->bindEvent('model.relation.add', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) {
* $relatedClass = get_class($relatedModel);
* $modelClass = get_class($model);
* traceLog("{$relatedClass} was added as {$relationName} to {$modelClass}.");
* });
*
*/
$this->parent->fireEvent('model.relation.add', [$this->relationName, $model]);
}
else {
$this->parent->bindDeferred($this->relationName, $model, $sessionKey);
}
}
/**
* remove a model from this relationship type.
*/
public function remove(Model $model, $sessionKey = null)
{
if ($sessionKey === null) {
/**
* @event model.relation.beforeRemove
* Called before removing a relation to the model (for AttachOneOrMany, HasOneOrMany & MorphOneOrMany relations)
*
* Example usage:
*
* $model->bindEvent('model.relation.beforeRemove', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) {
* if ($relationName === 'perm_relation') {
* return false;
* }
* });
*
*/
if ($this->parent->fireEvent('model.relation.beforeRemove', [$this->relationName, $model], true) === false) {
return;
}
if (!$this->isModelRemovable($model)) {
return;
}
$options = $this->parent->getRelationDefinition($this->relationName);
// Delete or orphan the model
if (Arr::get($options, 'delete', false)) {
$model->delete();
}
else {
$model->setAttribute($this->getForeignKeyName(), null);
$model->setAttribute($this->getMorphType(), null);
$model->save();
}
// Use this opportunity to set the relation in memory
if ($this instanceof MorphOne) {
$this->parent->setRelation($this->relationName, null);
}
else {
$this->parent->unsetRelation($this->relationName);
}
/**
* @event model.relation.remove
* Called after removing a relation to the model (for AttachOneOrMany, HasOneOrMany & MorphOneOrMany relations)
*
* Example usage:
*
* $model->bindEvent('model.relation.remove', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) {
* $relatedClass = get_class($relatedModel);
* $modelClass = get_class($model);
* traceLog("{$relatedClass} was removed from {$modelClass}.");
* });
*
*/
$this->parent->fireEvent('model.relation.remove', [$this->relationName, $model]);
}
else {
$this->parent->unbindDeferred($this->relationName, $model, $sessionKey);
}
}
/**
* isModelRemovable returns true if an existing model is already associated
*/
protected function isModelRemovable($model): bool
{
return
((string) $model->getAttribute($this->getForeignKeyName()) === (string) $this->getParentKey()) &&
$model->getAttribute($this->getMorphType()) === $this->morphClass;
}
/**
* ensureRelationIsEmpty ensures the relation is empty, either deleted or nulled.
*/
protected function ensureRelationIsEmpty()
{
$options = $this->parent->getRelationDefinition($this->relationName);
if (Arr::get($options, 'delete', false)) {
$this->delete();
}
else {
$this->update([
$this->getForeignKeyName() => null,
$this->getMorphType() => null
]);
}
}
/**
* getRelatedKeyName
* @return string
*/
public function getRelatedKeyName()
{
return $this->related->getKeyName();
}
}