Skip to content

Commit

Permalink
Fixes setting attach relation to null
Browse files Browse the repository at this point in the history
  • Loading branch information
daftspunk committed Apr 14, 2024
1 parent 5973b2a commit d24fda6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Database/Relations/AttachMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions src/Database/Relations/AttachOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions src/Database/Relations/AttachOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/Database/Relations/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions src/Database/Relations/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit d24fda6

Please sign in to comment.