Skip to content

Commit

Permalink
fix: relationships always being requested by various methods
Browse files Browse the repository at this point in the history
  • Loading branch information
saade committed Mar 20, 2024
1 parent 656dfc4 commit e928ee4
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 17 deletions.
16 changes: 12 additions & 4 deletions src/Forms/Components/Actions/AddAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,18 @@ protected function setUp(): void
);

$this->form(
fn (Component $component, Form $form): ?Form => match ($component->hasModal()) {
true => $component->getForm($form)
->model($component->getRelatedModel()),
default => null,
function (Component $component, Form $form): ?Form {
if(! $component->hasModal()) {
return null;
}

$form = $component->getForm($form);

if( $model = $component->getRelatedModel()) {
$form->model($model);
}

return $form;
}
);

Expand Down
18 changes: 13 additions & 5 deletions src/Forms/Components/Actions/AddChildAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,23 @@ protected function setUp(): void
);

$this->form(
fn (Component $component, Form $form): ?Form => match ($component->hasModal()) {
true => $component->getForm($form)
->model($component->getRelatedModel()),
default => null,
function (Component $component, Form $form): ?Form {
if(! $component->hasModal()) {
return null;
}

$form = $component->getForm($form);

if( $model = $component->getRelatedModel()) {
$form->model($model);
}

return $form;
}
);

$this->action(function (Component $component, array $arguments): void {
$parentRecord = $component->getCachedExistingRecords()->get($arguments['cachedRecordKey']);
$parentRecord = $component->getRelatedModel() ? $component->getCachedExistingRecords()->get($arguments['cachedRecordKey']) : null;

$this->process(function (Component $component, array $arguments, array $data): void {
$statePath = $component->getRelativeStatePath($arguments['statePath']);
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Components/Actions/DeleteAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected function setUp(): void
$this->modalSubmitActionLabel(fn (): string => __('filament-adjacency-list::adjacency-list.actions.delete.modal.actions.confirm'));

$this->action(function (Component $component, array $arguments): void {
$record = $component->getCachedExistingRecords()->get($arguments['cachedRecordKey']);
$record = $component->getRelatedModel() ? $component->getCachedExistingRecords()->get($arguments['cachedRecordKey']) : null;

$this->process(function (Component $component, array $arguments): void {
$statePath = $component->getRelativeStatePath($arguments['statePath']);
Expand Down
11 changes: 8 additions & 3 deletions src/Forms/Components/Actions/EditAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ protected function setUp(): void

$this->form(
function (Component $component, Form $form, array $arguments): Form {
return $component
$form = $component
->getForm($form)
->model($component->getCachedExistingRecords()->get($arguments['cachedRecordKey']))
->statePath($arguments['statePath']);

if( $component->getRelatedModel()) {
$form->model($component->getCachedExistingRecords()->get($arguments['cachedRecordKey']));
}

return $form;
}
);

Expand All @@ -43,7 +48,7 @@ function (Component $component, array $arguments): array {
);

$this->action(function (Component $component, array $arguments): void {
$record = $component->getCachedExistingRecords()->get($arguments['cachedRecordKey']);
$record = $component->getRelatedModel() ? $component->getCachedExistingRecords()->get($arguments['cachedRecordKey']) : null;

$this->process(function (Component $component, array $arguments, array $data): void {
$statePath = $component->getRelativeStatePath($arguments['statePath']);
Expand Down
14 changes: 10 additions & 4 deletions src/Forms/Components/Concerns/HasRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,15 +289,21 @@ public function getOrderColumn(): ?string
return $this->evaluate($this->orderColumn);
}

public function getRelationship(): HasMany | BelongsToMany
public function getRelationship(): HasMany | BelongsToMany | null
{
$name = $this->getRelationshipName();

if (blank($name)) {
return null;
}

if ($model = $this->getModelInstance()) {
if (! in_array(HasRecursiveRelationships::class, class_uses($model))) {
throw new \Exception('The model ' . $model::class . ' must use the ' . HasRecursiveRelationships::class . ' trait.');
}
}

return $model->{$this->getRelationshipName()}();
return $model->{$name}();
}

public function getRelationshipName(): ?string
Expand Down Expand Up @@ -347,9 +353,9 @@ public function clearCachedExistingRecords(): void
$this->cachedExistingRecords = null;
}

public function getRelatedModel(): string
public function getRelatedModel(): ?string
{
return $this->getRelationship()->getModel()::class;
return ($model = $this->getRelationship()?->getModel()) ? $model::class : null;
}

public function mutateRelationshipDataBeforeCreateUsing(?Closure $callback): static
Expand Down

0 comments on commit e928ee4

Please sign in to comment.