Skip to content

Commit

Permalink
Add detach association
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-libert committed Aug 28, 2024
1 parent ea005a0 commit 477f975
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/Api/Association.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ public function attach($targetId)
);
}

public function detach($targetId)
{
if ($targetId instanceof Model) {
$targetId = $targetId->id;
}

$this->sourceBuilder()->deleteAssociation(
$this->target, $targetId
);
}

public function sourceBuilder(): Builder
{
return $this->source->builder();
Expand All @@ -65,4 +76,4 @@ public function __call($method, $parameters)
{
return $this->forwardCallTo($this->builder(), $method, $parameters);
}
}
}
13 changes: 12 additions & 1 deletion src/Api/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,17 @@ public function associate(Model $target, $targetId)
)->json();
}

public function deleteAssociation(Model $target, $targetId)
{
return $this->client()->delete(
$this->object->endpoint('associate', [
'association' => $target->type(),
'associationId' => $targetId,
'associationType' => Str::singular($this->object->type()) . "_to_" . Str::singular($target->type())
])
)->json();
}

public function client(): Client
{
return $this->client;
Expand Down Expand Up @@ -408,4 +419,4 @@ public function __call($method, $parameters)
'Call to undefined method %s::%s()', static::class, $method
));
}
}
}
2 changes: 1 addition & 1 deletion src/Api/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function fill(array $properties): static

private function isAllowedProperty(string $key): bool
{
return $key === 'email' ||
return $key === 'email' ||
!(HubSpot::isType($key) || HubSpot::isType(Str::plural($key)));
}

Expand Down

0 comments on commit 477f975

Please sign in to comment.