From efe85fcd8c7902f6d955ba03abf67c3b7abfec30 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sat, 17 May 2025 10:07:00 -0400 Subject: [PATCH 1/8] Use pivot model table name if not defined in the relation --- src/Database/Concerns/HasRelationships.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Concerns/HasRelationships.php b/src/Database/Concerns/HasRelationships.php index 2373a01c8..7e7900a97 100644 --- a/src/Database/Concerns/HasRelationships.php +++ b/src/Database/Concerns/HasRelationships.php @@ -440,7 +440,7 @@ protected function handleRelation(string $relationName, bool $addConstraints = t case 'belongsToMany': $relation = $this->belongsToMany( $relatedClass, - $definition['table'] ?? null, + $definition['table'] ?? $definition['pivotModel'] ?? null, $definition['key'] ?? null, $definition['otherKey'] ?? null, $definition['parentKey'] ?? null, From 84ae11123c7ee5ad31333b744e813997f1ac04bf Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sat, 17 May 2025 10:53:44 -0400 Subject: [PATCH 2/8] add unit test for table defaulting to custom pivot model table --- .../Database/Relations/BelongsToManyTest.php | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/Database/Relations/BelongsToManyTest.php b/tests/Database/Relations/BelongsToManyTest.php index f149f54ca..ddf31b972 100644 --- a/tests/Database/Relations/BelongsToManyTest.php +++ b/tests/Database/Relations/BelongsToManyTest.php @@ -3,6 +3,7 @@ namespace Winter\Storm\Tests\Database\Relations; use Winter\Storm\Database\Model; +use Winter\Storm\Database\Pivot; use Winter\Storm\Support\Facades\DB; use Winter\Storm\Tests\Database\Fixtures\Category; use Winter\Storm\Tests\Database\Fixtures\Post; @@ -369,4 +370,27 @@ public function testConditionsWithPivotAttributes() $this->assertEquals([1, 2], $author->executiveAuthors()->lists('id')); $this->assertEquals([1, 2], $author->executiveAuthors()->get()->lists('id')); } + + function testTableDefaultsToCustomPivotTable() + { + $model = new TestModel(); + $relation = $model->{'dependencies'}(); + + $this->assertEquals('custom_pivot_table', $relation->getTable()); + } +} + +class TestModel extends Model +{ + public $belongsToMany = [ + 'dependencies' => [ + Model::class, + 'pivotModel' => TestCustomPivotModel::class, + ], + ]; +} + +class TestCustomPivotModel extends Pivot +{ + public $table = 'custom_pivot_table'; } From ef09bca8792e94b75a6b52249926a2a7e724ecbd Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sat, 17 May 2025 10:56:15 -0400 Subject: [PATCH 3/8] add missing public keyword --- tests/Database/Relations/BelongsToManyTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Database/Relations/BelongsToManyTest.php b/tests/Database/Relations/BelongsToManyTest.php index ddf31b972..11f14c461 100644 --- a/tests/Database/Relations/BelongsToManyTest.php +++ b/tests/Database/Relations/BelongsToManyTest.php @@ -371,7 +371,7 @@ public function testConditionsWithPivotAttributes() $this->assertEquals([1, 2], $author->executiveAuthors()->get()->lists('id')); } - function testTableDefaultsToCustomPivotTable() + public function testTableDefaultsToCustomPivotTable() { $model = new TestModel(); $relation = $model->{'dependencies'}(); From e4cbd6890b45df36092de0aaa4358ee9f5fa4574 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Wed, 21 May 2025 11:09:12 -0400 Subject: [PATCH 4/8] pivotmodel first --- src/Database/Concerns/HasRelationships.php | 4 +-- .../Database/Relations/BelongsToManyTest.php | 26 +++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/Database/Concerns/HasRelationships.php b/src/Database/Concerns/HasRelationships.php index 7e7900a97..79fe18baf 100644 --- a/src/Database/Concerns/HasRelationships.php +++ b/src/Database/Concerns/HasRelationships.php @@ -440,7 +440,7 @@ protected function handleRelation(string $relationName, bool $addConstraints = t case 'belongsToMany': $relation = $this->belongsToMany( $relatedClass, - $definition['table'] ?? $definition['pivotModel'] ?? null, + $definition['pivotModel'] ?? $definition['table'] ?? null, $definition['key'] ?? null, $definition['otherKey'] ?? null, $definition['parentKey'] ?? null, @@ -481,7 +481,7 @@ protected function handleRelation(string $relationName, bool $addConstraints = t $relation = $this->morphToMany( $relatedClass, $definition['name'] ?? $relationName, - $definition['table'] ?? null, + $definition['pivotModel'] ?? $definition['table'] ?? null, $definition['key'] ?? null, $definition['otherKey'] ?? null, $definition['parentKey'] ?? null, diff --git a/tests/Database/Relations/BelongsToManyTest.php b/tests/Database/Relations/BelongsToManyTest.php index 11f14c461..c74bc130b 100644 --- a/tests/Database/Relations/BelongsToManyTest.php +++ b/tests/Database/Relations/BelongsToManyTest.php @@ -374,7 +374,25 @@ public function testConditionsWithPivotAttributes() public function testTableDefaultsToCustomPivotTable() { $model = new TestModel(); - $relation = $model->{'dependencies'}(); + $model->addBelongsToManyRelation('pivot_with_table', [ + Model::class, + 'pivotModel' => CustomPivotWithTable::class, + ]); + + $relation = $model->{'pivot_with_table'}(); + + $this->assertEquals('custom_pivot_table', $relation->getTable()); + } + + public function testTableDefaultsToCustomPivotTableWithoutTable() + { + $model = new TestModel(); + $model->addBelongsToManyRelation('pivot_without_table', [ + Model::class, + 'pivotModel' => CustomPivotWithoutTable::class, + ]); + + $relation = $model->{'pivot_without_table'}(); $this->assertEquals('custom_pivot_table', $relation->getTable()); } @@ -390,7 +408,11 @@ class TestModel extends Model ]; } -class TestCustomPivotModel extends Pivot +class CustomPivotWithTable extends Pivot { public $table = 'custom_pivot_table'; } + +class CustomPivotWithoutTable extends Pivot +{ +} From 99d72df198508d6664b19ed73ee9a1de51bed507 Mon Sep 17 00:00:00 2001 From: Ben Thomson Date: Thu, 22 May 2025 00:57:03 +0000 Subject: [PATCH 5/8] Demonstrate correct able name --- tests/Database/Relations/BelongsToManyTest.php | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/tests/Database/Relations/BelongsToManyTest.php b/tests/Database/Relations/BelongsToManyTest.php index c74bc130b..817e30a92 100644 --- a/tests/Database/Relations/BelongsToManyTest.php +++ b/tests/Database/Relations/BelongsToManyTest.php @@ -379,7 +379,7 @@ public function testTableDefaultsToCustomPivotTable() 'pivotModel' => CustomPivotWithTable::class, ]); - $relation = $model->{'pivot_with_table'}(); + $relation = $model->pivot_with_table(); $this->assertEquals('custom_pivot_table', $relation->getTable()); } @@ -392,20 +392,14 @@ public function testTableDefaultsToCustomPivotTableWithoutTable() 'pivotModel' => CustomPivotWithoutTable::class, ]); - $relation = $model->{'pivot_without_table'}(); + $relation = $model->pivot_without_table(); - $this->assertEquals('custom_pivot_table', $relation->getTable()); + $this->assertEquals('custom_pivot_without_table', $relation->getTable()); } } class TestModel extends Model { - public $belongsToMany = [ - 'dependencies' => [ - Model::class, - 'pivotModel' => TestCustomPivotModel::class, - ], - ]; } class CustomPivotWithTable extends Pivot From 1a35249bd88e0814530fd70d85ea47dbb01fe75d Mon Sep 17 00:00:00 2001 From: Ben Thomson Date: Thu, 22 May 2025 01:28:08 +0000 Subject: [PATCH 6/8] Marc was right, Ben was wrong --- src/Database/Concerns/HasRelationships.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Concerns/HasRelationships.php b/src/Database/Concerns/HasRelationships.php index 79fe18baf..09f678b85 100644 --- a/src/Database/Concerns/HasRelationships.php +++ b/src/Database/Concerns/HasRelationships.php @@ -481,7 +481,7 @@ protected function handleRelation(string $relationName, bool $addConstraints = t $relation = $this->morphToMany( $relatedClass, $definition['name'] ?? $relationName, - $definition['pivotModel'] ?? $definition['table'] ?? null, + $definition['table'] ?? $definition['pivotModel'] ?? null, $definition['key'] ?? null, $definition['otherKey'] ?? null, $definition['parentKey'] ?? null, From 68f8f3d1a00f5edf63894f27cbd43565911b8252 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Wed, 21 May 2025 23:06:43 -0400 Subject: [PATCH 7/8] also restore BelongsToMany relation table assignment order --- src/Database/Concerns/HasRelationships.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Concerns/HasRelationships.php b/src/Database/Concerns/HasRelationships.php index 09f678b85..ee105850c 100644 --- a/src/Database/Concerns/HasRelationships.php +++ b/src/Database/Concerns/HasRelationships.php @@ -440,7 +440,7 @@ protected function handleRelation(string $relationName, bool $addConstraints = t case 'belongsToMany': $relation = $this->belongsToMany( $relatedClass, - $definition['pivotModel'] ?? $definition['table'] ?? null, + $definition['table'] ?? $definition['pivotModel'] ?? null, $definition['key'] ?? null, $definition['otherKey'] ?? null, $definition['parentKey'] ?? null, From 3d5e806829d036012c243020564962df140f8020 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Wed, 21 May 2025 23:15:17 -0400 Subject: [PATCH 8/8] add an extra test to make sure relation table has precedence --- tests/Database/Relations/BelongsToManyTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/Database/Relations/BelongsToManyTest.php b/tests/Database/Relations/BelongsToManyTest.php index 817e30a92..24bac0c0b 100644 --- a/tests/Database/Relations/BelongsToManyTest.php +++ b/tests/Database/Relations/BelongsToManyTest.php @@ -396,6 +396,20 @@ public function testTableDefaultsToCustomPivotTableWithoutTable() $this->assertEquals('custom_pivot_without_table', $relation->getTable()); } + + public function testTableDefaultsToRelationTable() + { + $model = new TestModel(); + $model->addBelongsToManyRelation('pivot_without_table', [ + Model::class, + 'table' => 'custom_pivot_table', + 'pivotModel' => CustomPivotWithoutTable::class, + ]); + + $relation = $model->pivot_without_table(); + + $this->assertEquals('custom_pivot_table', $relation->getTable()); + } } class TestModel extends Model