From 7aebe6a7c4ff22b6a5f9930ea2a8db74a78d74df Mon Sep 17 00:00:00 2001 From: Bez Hermoso Date: Thu, 31 Mar 2022 08:14:05 -0700 Subject: [PATCH 1/4] Add failing test --- ...gleTableInheritanceTraitCollectionTest.php | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 tests/SingleTableInheritanceTraitCollectionTest.php diff --git a/tests/SingleTableInheritanceTraitCollectionTest.php b/tests/SingleTableInheritanceTraitCollectionTest.php new file mode 100644 index 0000000..22cd921 --- /dev/null +++ b/tests/SingleTableInheritanceTraitCollectionTest.php @@ -0,0 +1,49 @@ +save(); + (new Car())->save(); + (new Truck())->save(); + (new Truck())->save(); + (new Bike())->save(); + + $results = Vehicle::all(); + + $this->assertEquals(5, count($results)); + + $this->assertInstanceOf('Nanigans\SingleTableInheritance\Tests\Fixtures\MotorVehicle', $results[0]); + $this->assertInstanceOf('Nanigans\SingleTableInheritance\Tests\Fixtures\Car', $results[1]); + $this->assertInstanceOf('Nanigans\SingleTableInheritance\Tests\Fixtures\Truck', $results[2]); + $this->assertInstanceOf('Nanigans\SingleTableInheritance\Tests\Fixtures\Truck', $results[3]); + $this->assertInstanceOf('Nanigans\SingleTableInheritance\Tests\Fixtures\Bike', $results[4]); + + $results = $results->fresh(); + + $this->assertInstanceOf('Nanigans\SingleTableInheritance\Tests\Fixtures\MotorVehicle', $results[0]); + $this->assertInstanceOf('Nanigans\SingleTableInheritance\Tests\Fixtures\Car', $results[1]); + $this->assertInstanceOf('Nanigans\SingleTableInheritance\Tests\Fixtures\Truck', $results[2]); + $this->assertInstanceOf('Nanigans\SingleTableInheritance\Tests\Fixtures\Truck', $results[3]); + $this->assertInstanceOf('Nanigans\SingleTableInheritance\Tests\Fixtures\Bike', $results[4]); + } +} From e307d49a725be7d99e52eafddd78c805774d8bcd Mon Sep 17 00:00:00 2001 From: Bez Hermoso Date: Thu, 31 Mar 2022 09:23:54 -0700 Subject: [PATCH 2/4] Implement Builder#getModels that produce polymorphic collection --- src/SingleInheritanceBuilderTrait.php | 52 +++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/SingleInheritanceBuilderTrait.php diff --git a/src/SingleInheritanceBuilderTrait.php b/src/SingleInheritanceBuilderTrait.php new file mode 100644 index 0000000..dbb6f4f --- /dev/null +++ b/src/SingleInheritanceBuilderTrait.php @@ -0,0 +1,52 @@ +$property; + return new $className(); + } + + /** + * This makes it so that polymorphic collections can be constructed properly. + * + * @param array|string $columns + * @return Model[]|static[] + */ + public function getModels($columns = ['*']) + { + /* + * The problem stems from Laravel using the first element in the collection to hydrate the rest of the records + * into Eloquent models. Thus, if any of the subsequent records do is of a type that does not inherit + * from that of the first element, then it fails. + * + * If we use an instance of the root model of the + * inheritance tree, then we can properly hydrate records into polymorphic models. + */ + $rootModel = $this->newSingleInheritanceRootModel(); + return $rootModel->hydrate( + $this->query->get($columns)->all() + )->all(); + } +} From 0df5ac0e4ca24a9aad34b82c9977ed36351c59aa Mon Sep 17 00:00:00 2001 From: Bez Hermoso Date: Thu, 31 Mar 2022 09:24:28 -0700 Subject: [PATCH 3/4] Implement fix in test fixture models --- tests/Fixtures/Vehicle.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/Fixtures/Vehicle.php b/tests/Fixtures/Vehicle.php index eff9c91..f223a90 100644 --- a/tests/Fixtures/Vehicle.php +++ b/tests/Fixtures/Vehicle.php @@ -66,4 +66,9 @@ public function setPrimaryKey($primaryKey) { public function setTable($table) { $this->table = $table; } -} \ No newline at end of file + + public function newEloquentBuilder($query) + { + return new VehicleBuilder($query); + } +} From da7857deb42197ddda57a0294e31063931411840 Mon Sep 17 00:00:00 2001 From: Bez Hermoso Date: Tue, 10 May 2022 14:16:28 -0700 Subject: [PATCH 4/4] VehicleBuilder (missing commit) --- tests/Fixtures/VehicleBuilder.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 tests/Fixtures/VehicleBuilder.php diff --git a/tests/Fixtures/VehicleBuilder.php b/tests/Fixtures/VehicleBuilder.php new file mode 100644 index 0000000..0f3c0db --- /dev/null +++ b/tests/Fixtures/VehicleBuilder.php @@ -0,0 +1,13 @@ +