Skip to content

Commit

Permalink
fix: allow loading relations that are not studly
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofandel committed May 8, 2024
1 parent 2f704f9 commit c0c58c7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
21 changes: 13 additions & 8 deletions src/Normalizers/Normalized/NormalizedModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,23 @@ protected function fetchNewProperty(string $name, DataProperty $dataProperty): m
return $this->properties[$name] = $this->model->getAttribute($name);
}

if (! $dataProperty->attributes->contains(fn (object $attribute) => $attribute::class === LoadRelation::class)) {
return UnknownProperty::create();
}

$studlyName = Str::studly($name);

if (! method_exists($this->model, $studlyName)) {
return UnknownProperty::create();
if ($dataProperty->attributes->contains(fn (object $attribute) => $attribute::class === LoadRelation::class)) {
if (method_exists($this->model, $name)) {
$this->model->loadMissing($name);
} else if (method_exists($this->model, $studlyName)) {
$this->model->loadMissing($studlyName);
}
}

$this->model->load($studlyName);
if ($this->model->relationLoaded($name)) {
return $this->properties[$name] = $this->model->getRelation($name);
}
if ($this->model->relationLoaded($studlyName)) {
return $this->properties[$name] = $this->model->getRelation($studlyName);
}

return $this->properties[$name] = $this->model->{$studlyName};
return UnknownProperty::create();
}
}
5 changes: 5 additions & 0 deletions tests/Fakes/Models/FakeModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public function fakeNestedModels(): HasMany
return $this->hasMany(FakeNestedModel::class);
}

public function alt_fake_nested_models(): HasMany
{
return $this->hasMany(FakeNestedModel::class);
}

public function accessor(): Attribute
{
return Attribute::get(fn () => "accessor_{$this->string}");
Expand Down
12 changes: 10 additions & 2 deletions tests/Normalizers/ModelNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,12 @@
$dataClass = new class () extends Data {
#[LoadRelation, DataCollectionOf(FakeNestedModelData::class)]
public array $fake_nested_models;

#[LoadRelation, DataCollectionOf(FakeNestedModelData::class)]
public array $alt_fake_nested_models;
};

$model->load('alt_fake_nested_models');
DB::enableQueryLog();

$data = $dataClass::from($model);
Expand All @@ -114,6 +118,10 @@
expect($data->fake_nested_models)
->toHaveCount(2)
->each->toBeInstanceOf(FakeNestedModelData::class);

expect($data->alt_fake_nested_models)
->toHaveCount(2)
->each->toBeInstanceOf(FakeNestedModelData::class);
expect($queryLog)->toHaveCount(1);
});

Expand Down Expand Up @@ -154,8 +162,8 @@
it('can use mappers to map the names', function () {
$model = FakeModel::factory()->create();

$nestedModelA = FakeNestedModel::factory()->for($model)->create();
$nestedModelB = FakeNestedModel::factory()->for($model)->create();
FakeNestedModel::factory()->for($model)->create();
FakeNestedModel::factory()->for($model)->create();

$dataClass = new class () extends Data {
#[DataCollectionOf(FakeNestedModelData::class), MapInputName(SnakeCaseMapper::class)]
Expand Down

0 comments on commit c0c58c7

Please sign in to comment.