-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathNestedOneToMany.php
More file actions
141 lines (116 loc) · 4.44 KB
/
NestedOneToMany.php
File metadata and controls
141 lines (116 loc) · 4.44 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
<?php
namespace Nuwave\Lighthouse\Execution\Arguments;
use Closure;
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
use Illuminate\Database\Eloquent\Relations\Relation;
use Nuwave\Lighthouse\Support\Contracts\ArgResolver;
class NestedOneToMany implements ArgResolver
{
/**
* @var string
*/
protected $relationName;
public function __construct(string $relationName)
{
$this->relationName = $relationName;
}
/**
* @param \Illuminate\Database\Eloquent\Model $parent
* @param \Nuwave\Lighthouse\Execution\Arguments\ArgumentSet $args
*/
public function __invoke($parent, $args): void
{
/** @var \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\MorphMany $relation */
$relation = $parent->{$this->relationName}();
static::createUpdateUpsert($args, $relation);
static::connectDisconnect($args, $relation);
if ($args->has('delete')) {
$relation->getRelated()::destroy(
$args->arguments['delete']->toPlain()
);
}
}
public static function createUpdateUpsert(ArgumentSet $args, Relation $relation): void
{
if ($args->has('create')) {
$saveModel = new ResolveNested(new SaveModel($relation));
foreach ($args->arguments['create']->value as $childArgs) {
// @phpstan-ignore-next-line Relation&Builder mixin not recognized
$saveModel($relation->make(), $childArgs);
}
}
if ($args->has('update')) {
$updateModel = new ResolveNested(new SaveModel($relation));
// @phpstan-ignore-next-line Relation&Builder mixin not recognized
$model = $relation->make();
$ids = [];
$updateArgs = $args->arguments['update']->value;
foreach ($updateArgs as $childArgs) {
$ids[UpdateModel::getId($childArgs, $model)] = true;
}
$models = $model->newModelQuery()
->whereIn($model->getKeyName(), array_keys($ids))
->get();
foreach ($models as $instance) {
$id = $instance->getKey();
$ids[$id] = $instance;
}
foreach ($updateArgs as $childArgs) {
$instance = $ids[UpdateModel::pullId($childArgs, $model)];
$updateModel($instance, $childArgs);
}
}
if ($args->has('upsert')) {
$upsertModel = new ResolveNested(new UpsertModel(new SaveModel($relation)));
foreach ($args->arguments['upsert']->value as $childArgs) {
// @phpstan-ignore-next-line Relation&Builder mixin not recognized
$upsertModel($relation->make(), $childArgs);
}
}
}
public static function connectDisconnect(ArgumentSet $args, HasOneOrMany $relation): void
{
if ($args->has('connect')) {
// @phpstan-ignore-next-line Relation&Builder mixin not recognized
$children = $relation
->make()
->whereIn(
self::getLocalKeyName($relation),
$args->arguments['connect']->value
)
->get();
// @phpstan-ignore-next-line Relation&Builder mixin not recognized
$relation->saveMany($children);
}
if ($args->has('disconnect')) {
// @phpstan-ignore-next-line Relation&Builder mixin not recognized
$children = $relation
->make()
->whereIn(
self::getLocalKeyName($relation),
$args->arguments['disconnect']->value
)
->get();
/** @var \Illuminate\Database\Eloquent\Model $child */
foreach ($children as $child) {
$child->setAttribute($relation->getForeignKeyName(), null);
$child->save();
}
}
}
/**
* TODO remove this horrible hack when we no longer support Laravel 5.6.
*/
protected static function getLocalKeyName(HasOneOrMany $relation): string
{
$getLocalKeyName = Closure::bind(
function () {
// @phpstan-ignore-next-line This is a dirty hack
return $this->localKey;
},
$relation,
get_class($relation)
);
return $getLocalKeyName();
}
}