From c0c58c794c874b29107c9b0db71cfc57ecdcd29f Mon Sep 17 00:00:00 2001 From: Tofandel Date: Wed, 8 May 2024 13:31:27 +0200 Subject: [PATCH] fix: allow loading relations that are not studly --- .../Normalized/NormalizedModel.php | 21 ++++++++++++------- tests/Fakes/Models/FakeModel.php | 5 +++++ tests/Normalizers/ModelNormalizerTest.php | 12 +++++++++-- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/Normalizers/Normalized/NormalizedModel.php b/src/Normalizers/Normalized/NormalizedModel.php index fbc46cb5..2a0df013 100644 --- a/src/Normalizers/Normalized/NormalizedModel.php +++ b/src/Normalizers/Normalized/NormalizedModel.php @@ -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(); } } diff --git a/tests/Fakes/Models/FakeModel.php b/tests/Fakes/Models/FakeModel.php index 095bb7a7..a37f2bb7 100644 --- a/tests/Fakes/Models/FakeModel.php +++ b/tests/Fakes/Models/FakeModel.php @@ -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}"); diff --git a/tests/Normalizers/ModelNormalizerTest.php b/tests/Normalizers/ModelNormalizerTest.php index df701e04..f935456c 100644 --- a/tests/Normalizers/ModelNormalizerTest.php +++ b/tests/Normalizers/ModelNormalizerTest.php @@ -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); @@ -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); }); @@ -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)]