From 1ff1187f6090fff5124f306ac3b1b0e123672f5f Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 11 Aug 2021 15:18:05 -0500 Subject: [PATCH 1/4] support class basenames on morphs --- .../Eloquent/Concerns/HasRelationships.php | 4 +++- .../Database/Eloquent/Relations/Relation.php | 17 +++++++++++++++++ tests/Database/DatabaseEloquentModelTest.php | 10 ++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php index 5262d4305273..9b2e802173ca 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php @@ -731,7 +731,9 @@ public function getMorphClass() return array_search(static::class, $morphMap, true); } - return static::class; + return Relation::$useClassBasenamesForMorphMap + ? class_basename(static::class) + : static::class; } /** diff --git a/src/Illuminate/Database/Eloquent/Relations/Relation.php b/src/Illuminate/Database/Eloquent/Relations/Relation.php index 7fe9f3e9fa82..d672a2e73b15 100755 --- a/src/Illuminate/Database/Eloquent/Relations/Relation.php +++ b/src/Illuminate/Database/Eloquent/Relations/Relation.php @@ -50,6 +50,13 @@ abstract class Relation */ protected static $constraints = true; + /** + * Indicates that class basenames should be used when determining morph classes. + * + * @var bool + */ + public static $useClassBasenamesForMorphMap = false; + /** * An array to map class names to their morph names in the database. * @@ -376,6 +383,16 @@ protected function whereInMethod(Model $model, $key) : 'whereIn'; } + /** + * Indicate that the basename of classes should be used when determining morphed class names. + * + * @return void + */ + public static function useClassBasenamesForMorphMap() + { + static::$useClassBasenamesForMorphMap = true; + } + /** * Set or get the morph map for polymorphic relations. * diff --git a/tests/Database/DatabaseEloquentModelTest.php b/tests/Database/DatabaseEloquentModelTest.php index a36bba4fe3eb..d616bc8050c1 100755 --- a/tests/Database/DatabaseEloquentModelTest.php +++ b/tests/Database/DatabaseEloquentModelTest.php @@ -1217,6 +1217,16 @@ public function testMorphOneCreatesProperRelation() $this->assertEquals(EloquentModelStub::class, $relation->getMorphClass()); } + public function testMorphOneCreatesProperRelationWhenUsingBasenames() + { + Relation::useClassBasenamesForMorphMap(); + $model = new EloquentModelStub; + $this->addMockConnection($model); + $relation = $model->morphOne(EloquentModelSaveStub::class, 'morph'); + $this->assertEquals('EloquentModelStub', $relation->getMorphClass()); + Relation::$useClassBasenamesForMorphMap = false; + } + public function testCorrectMorphClassIsReturned() { Relation::morphMap(['alias' => 'AnotherModel']); From d0e6d90635a1b2ba2820ab57fee5a058c14ecea4 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 19 Aug 2021 09:09:18 -0500 Subject: [PATCH 2/4] table name option for morphmap --- .../Database/Eloquent/Concerns/HasRelationships.php | 4 ++-- src/Illuminate/Database/Eloquent/Relations/Relation.php | 8 ++++---- tests/Database/DatabaseEloquentModelTest.php | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php index 9b2e802173ca..993223d1147b 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php @@ -731,8 +731,8 @@ public function getMorphClass() return array_search(static::class, $morphMap, true); } - return Relation::$useClassBasenamesForMorphMap - ? class_basename(static::class) + return Relation::$useTableNamesForMorphMap + ? $this->getTable() : static::class; } diff --git a/src/Illuminate/Database/Eloquent/Relations/Relation.php b/src/Illuminate/Database/Eloquent/Relations/Relation.php index d672a2e73b15..61d9abf2fef5 100755 --- a/src/Illuminate/Database/Eloquent/Relations/Relation.php +++ b/src/Illuminate/Database/Eloquent/Relations/Relation.php @@ -51,11 +51,11 @@ abstract class Relation protected static $constraints = true; /** - * Indicates that class basenames should be used when determining morph classes. + * Indicates that model table names should be used when determining morph classes. * * @var bool */ - public static $useClassBasenamesForMorphMap = false; + public static $useTableNamesForMorphMap = false; /** * An array to map class names to their morph names in the database. @@ -388,9 +388,9 @@ protected function whereInMethod(Model $model, $key) * * @return void */ - public static function useClassBasenamesForMorphMap() + public static function useTableNamesForMorphMap() { - static::$useClassBasenamesForMorphMap = true; + static::$useTableNamesForMorphMap = true; } /** diff --git a/tests/Database/DatabaseEloquentModelTest.php b/tests/Database/DatabaseEloquentModelTest.php index d616bc8050c1..853302a17fa0 100755 --- a/tests/Database/DatabaseEloquentModelTest.php +++ b/tests/Database/DatabaseEloquentModelTest.php @@ -1219,12 +1219,12 @@ public function testMorphOneCreatesProperRelation() public function testMorphOneCreatesProperRelationWhenUsingBasenames() { - Relation::useClassBasenamesForMorphMap(); + Relation::useTableNamesForMorphMap(); $model = new EloquentModelStub; $this->addMockConnection($model); $relation = $model->morphOne(EloquentModelSaveStub::class, 'morph'); - $this->assertEquals('EloquentModelStub', $relation->getMorphClass()); - Relation::$useClassBasenamesForMorphMap = false; + $this->assertEquals('stub', $relation->getMorphClass()); + Relation::$useTableNamesForMorphMap = false; } public function testCorrectMorphClassIsReturned() From 5dca4e377647563d1ccde84d2819a11c16a4c952 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 19 Aug 2021 09:13:18 -0500 Subject: [PATCH 3/4] rename method --- src/Illuminate/Database/Eloquent/Relations/Relation.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/Relation.php b/src/Illuminate/Database/Eloquent/Relations/Relation.php index 61d9abf2fef5..c1e9a482cdc3 100755 --- a/src/Illuminate/Database/Eloquent/Relations/Relation.php +++ b/src/Illuminate/Database/Eloquent/Relations/Relation.php @@ -51,7 +51,7 @@ abstract class Relation protected static $constraints = true; /** - * Indicates that model table names should be used when determining morph classes. + * Indicates that table names should be used when determining morph classes. * * @var bool */ @@ -384,11 +384,11 @@ protected function whereInMethod(Model $model, $key) } /** - * Indicate that the basename of classes should be used when determining morphed class names. + * Indicate that the table names should be used when determining morphed class names. * * @return void */ - public static function useTableNamesForMorphMap() + public static function morphUsingTableNames() { static::$useTableNamesForMorphMap = true; } From 1bb64ff57e1ed453a2605ed4ff7ad656dc05183c Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 19 Aug 2021 09:19:59 -0500 Subject: [PATCH 4/4] fix test --- tests/Database/DatabaseEloquentModelTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Database/DatabaseEloquentModelTest.php b/tests/Database/DatabaseEloquentModelTest.php index 853302a17fa0..88fb3b7de0a9 100755 --- a/tests/Database/DatabaseEloquentModelTest.php +++ b/tests/Database/DatabaseEloquentModelTest.php @@ -1217,9 +1217,9 @@ public function testMorphOneCreatesProperRelation() $this->assertEquals(EloquentModelStub::class, $relation->getMorphClass()); } - public function testMorphOneCreatesProperRelationWhenUsingBasenames() + public function testMorphOneCreatesProperRelationWhenUsingTableNames() { - Relation::useTableNamesForMorphMap(); + Relation::morphUsingTableNames(); $model = new EloquentModelStub; $this->addMockConnection($model); $relation = $model->morphOne(EloquentModelSaveStub::class, 'morph');