diff --git a/src/Database/Relations/AttachMany.php b/src/Database/Relations/AttachMany.php index e57465d1b..aca72342c 100644 --- a/src/Database/Relations/AttachMany.php +++ b/src/Database/Relations/AttachMany.php @@ -39,6 +39,18 @@ public function __construct(Builder $query, Model $parent, $type, $id, $isPublic */ public function setSimpleValue($value) { + // Nulling the relationship + if (!$value) { + $this->parent->setRelation($this->relationName, null); + + if ($this->parent->exists) { + $this->parent->bindEventOnce('model.afterSave', function() { + $this->ensureRelationIsEmpty(); + }); + } + return; + } + // Append a single newly uploaded file(s) if ($value instanceof UploadedFile) { $this->parent->bindEventOnce('model.afterSave', function () use ($value) { diff --git a/src/Database/Relations/AttachOne.php b/src/Database/Relations/AttachOne.php index f7e2a65a7..0ef78b687 100644 --- a/src/Database/Relations/AttachOne.php +++ b/src/Database/Relations/AttachOne.php @@ -43,6 +43,18 @@ public function setSimpleValue($value) $value = reset($value); } + // Nulling the relationship + if (!$value) { + $this->parent->setRelation($this->relationName, null); + + if ($this->parent->exists) { + $this->parent->bindEventOnce('model.afterSave', function() { + $this->ensureRelationIsEmpty(); + }); + } + return; + } + // Newly uploaded file if ($value instanceof UploadedFile) { $this->parent->bindEventOnce('model.afterSave', function () use ($value) { diff --git a/src/Database/Relations/AttachOneOrMany.php b/src/Database/Relations/AttachOneOrMany.php index 6901ea613..e6f0ad40f 100644 --- a/src/Database/Relations/AttachOneOrMany.php +++ b/src/Database/Relations/AttachOneOrMany.php @@ -404,6 +404,21 @@ public function makeValidationFile($value) return $value; } + /** + * ensureRelationIsEmpty ensures the relation is empty, either deleted or nulled. + */ + protected function ensureRelationIsEmpty() + { + $options = $this->parent->getRelationDefinition($this->relationName); + + if (array_get($options, 'delete', false)) { + $this->delete(); + } + else { + $this->update([$this->getForeignKeyName() => null]); + } + } + /** * getRelatedKeyName * @return string diff --git a/src/Database/Relations/HasMany.php b/src/Database/Relations/HasMany.php index d10f9e969..dbad14e30 100644 --- a/src/Database/Relations/HasMany.php +++ b/src/Database/Relations/HasMany.php @@ -37,6 +37,8 @@ public function setSimpleValue($value) { // Nulling the relationship if (!$value) { + $this->parent->setRelation($this->relationName, null); + if ($this->parent->exists) { $this->parent->bindEventOnce('model.afterSave', function() { $this->ensureRelationIsEmpty(); diff --git a/src/Database/Relations/HasOne.php b/src/Database/Relations/HasOne.php index 57db078c6..65c333760 100644 --- a/src/Database/Relations/HasOne.php +++ b/src/Database/Relations/HasOne.php @@ -39,6 +39,8 @@ public function setSimpleValue($value) // Nulling the relationship if (!$value) { + $this->parent->setRelation($this->relationName, null); + if ($this->parent->exists) { $this->parent->bindEventOnce('model.afterSave', function() { $this->ensureRelationIsEmpty();