Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populating nested mutations with other relationships for @update #549

Merged
merged 25 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
991d21b
refactoring tests
liepaja Jan 5, 2019
0581a84
Merge branch 'master' into nested-mutations-for-update
spawnia Jan 21, 2019
3df73fe
Fix BelongsToTest
spawnia Jan 21, 2019
8a6250f
Merge remote-tracking branch 'upstream/master' into nested-mutations-…
liepaja Feb 8, 2019
723183b
Porting functionality from create to update
liepaja Feb 9, 2019
5a98b9f
HasOne
liepaja Feb 9, 2019
2886b00
Merge remote-tracking branch 'upstream/master' into nested-mutations-…
liepaja Feb 28, 2019
ebbc716
Merge branch 'master' into liepaja-nested-mutations-for-update
spawnia Mar 7, 2019
63386a9
Merge remote-tracking branch 'liepaja/nested-mutations-for-update' in…
spawnia Mar 7, 2019
f8f9287
Fix type hints
spawnia Mar 7, 2019
a6adb35
Detach before destroying a many-to-many relationship
spawnia Mar 7, 2019
83c1a07
Add connect, sync and disconnect for many-to-many relationships
spawnia Mar 7, 2019
018d1c8
Add some docs for updating nested mutations
spawnia Mar 13, 2019
4dce8a4
Add test for BelongsToMany and fix it
spawnia Mar 14, 2019
2c91398
zYN7rK
spawnia Mar 14, 2019
f836de1
Merge branch 'master' into liepaja-nested-mutations-for-update
spawnia Mar 14, 2019
d2eb553
Add docs for updating a belongsTo relation
spawnia Mar 15, 2019
a3f267c
Merge branch 'master' into liepaja-nested-mutations-for-update
spawnia Mar 18, 2019
575f2c1
Clarify semantics for deleting/disconnecting BelongsTo, adding tests …
spawnia Mar 18, 2019
dd3e72b
Use new Collection instead of collect helper
spawnia Mar 19, 2019
035d98b
Make test table columns nullable for ease of use in testing
spawnia Mar 19, 2019
8383c7c
Fix column name in HourFactory
spawnia Mar 19, 2019
9f0c53a
Fix treatment of nested relation args for HasOne and MorphOne relations
spawnia Mar 19, 2019
1032029
Combine nested relation execution tests by relation type, simplifying…
spawnia Mar 19, 2019
be3c39e
zOGM7B
spawnia Mar 19, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add test for BelongsToMany and fix it
  • Loading branch information
spawnia committed Mar 14, 2019
commit 4dce8a4b4e493a7b59e2a5b6ba2492e77822b59d
44 changes: 32 additions & 12 deletions src/Execution/MutationExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static function executeCreate(Model $model, Collection $args, ?Relation $

[$morphToMany, $remaining] = self::partitionArgsByRelationType($reflection, $remaining, MorphToMany::class);

$model = self::saveModelWithBelongsTo($model, $remaining, $parentRelation);
$model = self::saveModelWithPotentialParent($model, $remaining, $parentRelation);

self::executeCreateHasMany($model, $hasMany);

Expand All @@ -62,17 +62,20 @@ public static function executeCreate(Model $model, Collection $args, ?Relation $
}

/**
* Save a model with BelongsTo.
* Save a model that maybe has a parent.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param \Illuminate\Support\Collection $args
* @param \Illuminate\Database\Eloquent\Relations\Relation|null $parentRelation
* @return \Illuminate\Database\Eloquent\Model
*/
protected static function saveModelWithBelongsTo(Model $model, Collection $args, ?Relation $parentRelation = null): Model
protected static function saveModelWithPotentialParent(Model $model, Collection $args, ?Relation $parentRelation = null): Model
{
$reflection = new ReflectionClass($model);
[$belongsTo, $remaining] = self::partitionArgsByRelationType($reflection, $args, BelongsTo::class);
[$belongsTo, $remaining] = self::partitionArgsByRelationType(
new ReflectionClass($model),
$args,
BelongsTo::class
);

// Use all the remaining attributes and fill the model
$model->fill(
Expand All @@ -96,18 +99,35 @@ protected static function saveModelWithBelongsTo(Model $model, Collection $args,
$belongsTo->associate($values);
}

if ($operationKey === 'update') {
$belongsToModel = self::executeUpdate($relation->getModel()->newInstance(), collect($values));
$relation->associate($belongsToModel);
}

if ($operationKey === 'disconnect') {
$relation->dissociate();
}

if ($operationKey === 'delete') {
$relation->getModel()::destroy($values);
}
});
});

// If we are already resolving a nested create, we might
// already have an instance of the parent relation available.
// In that case, use it to set the current model as a child.
$parentRelation
? $parentRelation->save($model)
: $model->save();
if ($parentRelation && ! $parentRelation instanceof BelongsToMany){
// If we are already resolving a nested create, we might
// already have an instance of the parent relation available.
// In that case, use it to set the current model as a child.
$parentRelation->save($model);

return $model;
}

$model->save();

if ($parentRelation instanceof BelongsToMany) {
$parentRelation->syncWithoutDetaching($model);
}

return $model;
}
Expand Down Expand Up @@ -180,7 +200,7 @@ public static function executeUpdate(Model $model, Collection $args, ?Relation $

[$morphToMany, $remaining] = self::partitionArgsByRelationType($reflection, $remaining, MorphToMany::class);

$model = self::saveModelWithBelongsTo($model, $remaining, $parentRelation);
$model = self::saveModelWithPotentialParent($model, $remaining, $parentRelation);

$hasMany->each(function (array $nestedOperations, string $relationName) use ($model): void {
/** @var \Illuminate\Database\Eloquent\Relations\HasMany $relation */
Expand Down
Loading