From 0f25d9b4771d919869c607545b4b5ef48c1a99d7 Mon Sep 17 00:00:00 2001 From: ahmed khattab <35297914+khattab17@users.noreply.github.com> Date: Thu, 4 Mar 2021 17:29:49 +0200 Subject: [PATCH 1/9] Update RelationHelper.php rename_hasMany_model_relation --- src/Coders/Model/Relations/RelationHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Coders/Model/Relations/RelationHelper.php b/src/Coders/Model/Relations/RelationHelper.php index 08006d7d..5cd31fa5 100644 --- a/src/Coders/Model/Relations/RelationHelper.php +++ b/src/Coders/Model/Relations/RelationHelper.php @@ -24,7 +24,7 @@ public static function stripSuffixFromForeignKey($usesSnakeAttributes, $primaryK return preg_replace('/(_)(' . $primaryKey . '|' . $lowerPrimaryKey . ')$/', '', $foreignKey); } else { $studlyPrimaryKey = Str::studly($primaryKey); - return preg_replace('/(' . $primaryKey . '|' . $studlyPrimaryKey . ')$/', '', $foreignKey); + return preg_replace('/(_)(' . $primaryKey . '|' . $studlyPrimaryKey . ')$/', '', $foreignKey); } } } From ca823a005261b5901227022e864d8bf2dd30d51e Mon Sep 17 00:00:00 2001 From: ahmed khattab Date: Thu, 4 Mar 2021 17:36:57 +0200 Subject: [PATCH 2/9] fix hasMany Models relation name --- src/Coders/Model/Relations/HasMany.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Coders/Model/Relations/HasMany.php b/src/Coders/Model/Relations/HasMany.php index ff8d451f..313f1921 100644 --- a/src/Coders/Model/Relations/HasMany.php +++ b/src/Coders/Model/Relations/HasMany.php @@ -35,7 +35,7 @@ public function name() if (Str::snake($relationName) === Str::snake($this->parent->getClassName())) { $relationName = Str::plural($this->related->getClassName()); } else { - $relationName = Str::plural($this->related->getClassName()) . 'Where' . ucfirst(Str::singular($relationName)); + $relationName = Str::plural($relationName); } break; default: From f03ed913447a79e11f2945650b05cf3c09d4c379 Mon Sep 17 00:00:00 2001 From: ahmed khattab Date: Fri, 5 Mar 2021 14:50:54 +0200 Subject: [PATCH 3/9] add foreign_key_field_name option to config/models.php --- config/models.php | 20 ++++++++++++++++++-- src/Coders/Model/Relations/BelongsTo.php | 1 + src/Coders/Model/Relations/HasMany.php | 14 +++++++++++++- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/config/models.php b/config/models.php index 9561e428..0809d99e 100644 --- a/config/models.php +++ b/config/models.php @@ -346,10 +346,26 @@ | Where the foreign key matches the related table name, it behaves as per the 'related' strategy. | (post.user_id --> user.id) | generates Post::user() and User::posts() + | + | 'foreign_key_field_name' Use the foreign key as the relation name without its parent table name. + | This can help to provide more meaningful relationship names, and avoids naming conflicts + | if you have more than one relationship between two tables. + | (post.author_id --> user.id) + | generates Post::author() and User::authors() + | (post.editor_id --> user.id) + | generates Post::editor() and User::editor() + | ID suffixes can be omitted from foreign keys. + | (post.author --> user.id) + | (post.editor --> user.id) + | generates the same as above. + | Where the foreign key matches the related table name, it behaves as per the 'related' strategy. + | (post.user_id --> user.id) + | generates Post::user() and User::posts() */ - - 'relation_name_strategy' => 'related', + + //'relation_name_strategy' => 'related', // 'relation_name_strategy' => 'foreign_key', + 'relation_name_strategy' => 'foreign_key_field_name', /* |-------------------------------------------------------------------------- diff --git a/src/Coders/Model/Relations/BelongsTo.php b/src/Coders/Model/Relations/BelongsTo.php index 5c6cf3e1..498b3d69 100644 --- a/src/Coders/Model/Relations/BelongsTo.php +++ b/src/Coders/Model/Relations/BelongsTo.php @@ -51,6 +51,7 @@ public function name() { switch ($this->parent->getRelationNameStrategy()) { case 'foreign_key': + case 'foreign_key_field_name': $relationName = RelationHelper::stripSuffixFromForeignKey( $this->parent->usesSnakeAttributes(), $this->otherKey(), diff --git a/src/Coders/Model/Relations/HasMany.php b/src/Coders/Model/Relations/HasMany.php index 313f1921..8ad2c552 100644 --- a/src/Coders/Model/Relations/HasMany.php +++ b/src/Coders/Model/Relations/HasMany.php @@ -26,7 +26,7 @@ public function hint() public function name() { switch ($this->parent->getRelationNameStrategy()) { - case 'foreign_key': + case 'foreign_key_field_name': $relationName = RelationHelper::stripSuffixFromForeignKey( $this->parent->usesSnakeAttributes(), $this->localKey(), @@ -38,6 +38,18 @@ public function name() $relationName = Str::plural($relationName); } break; + case 'foreign_key': + $relationName = RelationHelper::stripSuffixFromForeignKey( + $this->parent->usesSnakeAttributes(), + $this->localKey(), + $this->foreignKey() + ); + if (Str::snake($relationName) === Str::snake($this->parent->getClassName())) { + $relationName = Str::plural($this->related->getClassName()); + } else { + $relationName = Str::plural($this->related->getClassName()) . 'Where' . ucfirst(Str::singular($relationName)); + } + break; default: case 'related': $relationName = Str::plural($this->related->getClassName()); From 1dcab5b947a570fa342c59122377f9a9e5728329 Mon Sep 17 00:00:00 2001 From: ahmed khattab Date: Fri, 5 Mar 2021 17:38:52 +0200 Subject: [PATCH 4/9] set snake_attributes to false --- config/models.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/models.php b/config/models.php index 0809d99e..1d0d3bc5 100644 --- a/config/models.php +++ b/config/models.php @@ -183,7 +183,7 @@ | */ - 'snake_attributes' => true, + 'snake_attributes' => false, /* |-------------------------------------------------------------------------- From 824185f2263f501421511fed20ee71e35f27d434 Mon Sep 17 00:00:00 2001 From: ahmed khattab Date: Fri, 5 Mar 2021 20:20:43 +0200 Subject: [PATCH 5/9] fix hasMany Relation name --- src/Coders/Model/Relations/HasMany.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Coders/Model/Relations/HasMany.php b/src/Coders/Model/Relations/HasMany.php index 8ad2c552..231535c5 100644 --- a/src/Coders/Model/Relations/HasMany.php +++ b/src/Coders/Model/Relations/HasMany.php @@ -35,7 +35,7 @@ public function name() if (Str::snake($relationName) === Str::snake($this->parent->getClassName())) { $relationName = Str::plural($this->related->getClassName()); } else { - $relationName = Str::plural($relationName); + $relationName = ucfirst(Str::singular($relationName)).Str::plural($this->related->getClassName()); } break; case 'foreign_key': From 71d511cdecc40e5bfbc9a65bab564c93b5a4fe84 Mon Sep 17 00:00:00 2001 From: ahmed khattab <35297914+khattab17@users.noreply.github.com> Date: Sun, 7 Mar 2021 00:53:54 +0200 Subject: [PATCH 6/9] Update models.php --- config/models.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/models.php b/config/models.php index 1d0d3bc5..0809d99e 100644 --- a/config/models.php +++ b/config/models.php @@ -183,7 +183,7 @@ | */ - 'snake_attributes' => false, + 'snake_attributes' => true, /* |-------------------------------------------------------------------------- From c21f4ed8e0f277225a8738aeb54bc6278fde06fb Mon Sep 17 00:00:00 2001 From: ahmed khattab <35297914+khattab17@users.noreply.github.com> Date: Sun, 7 Mar 2021 00:56:20 +0200 Subject: [PATCH 7/9] Update models.php --- config/models.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/config/models.php b/config/models.php index 0809d99e..97f47308 100644 --- a/config/models.php +++ b/config/models.php @@ -362,8 +362,7 @@ | (post.user_id --> user.id) | generates Post::user() and User::posts() */ - - //'relation_name_strategy' => 'related', + // 'relation_name_strategy' => 'related', // 'relation_name_strategy' => 'foreign_key', 'relation_name_strategy' => 'foreign_key_field_name', From 7c697b29a6bfb8f68e9a5f2d310e4b5f3e245918 Mon Sep 17 00:00:00 2001 From: ahmed khattab Date: Mon, 8 Mar 2021 17:21:18 +0200 Subject: [PATCH 8/9] fix stripSuffixFromForeignKey function test bug --- src/Coders/Model/Relations/RelationHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Coders/Model/Relations/RelationHelper.php b/src/Coders/Model/Relations/RelationHelper.php index 5cd31fa5..60dd3ee9 100644 --- a/src/Coders/Model/Relations/RelationHelper.php +++ b/src/Coders/Model/Relations/RelationHelper.php @@ -24,7 +24,7 @@ public static function stripSuffixFromForeignKey($usesSnakeAttributes, $primaryK return preg_replace('/(_)(' . $primaryKey . '|' . $lowerPrimaryKey . ')$/', '', $foreignKey); } else { $studlyPrimaryKey = Str::studly($primaryKey); - return preg_replace('/(_)(' . $primaryKey . '|' . $studlyPrimaryKey . ')$/', '', $foreignKey); + return preg_replace('/(' . $primaryKey . '|' . $studlyPrimaryKey . '| (_)'. $primaryKey.')$/', '', $foreignKey); } } } From 11af7b7fd53edd9bbc11f82e72de2cb6d68c2c7a Mon Sep 17 00:00:00 2001 From: ahmed khattab Date: Mon, 8 Mar 2021 17:25:59 +0200 Subject: [PATCH 9/9] fix stripSuffixFromForeignKey function test bug --- src/Coders/Model/Relations/RelationHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Coders/Model/Relations/RelationHelper.php b/src/Coders/Model/Relations/RelationHelper.php index 60dd3ee9..c8409046 100644 --- a/src/Coders/Model/Relations/RelationHelper.php +++ b/src/Coders/Model/Relations/RelationHelper.php @@ -24,7 +24,7 @@ public static function stripSuffixFromForeignKey($usesSnakeAttributes, $primaryK return preg_replace('/(_)(' . $primaryKey . '|' . $lowerPrimaryKey . ')$/', '', $foreignKey); } else { $studlyPrimaryKey = Str::studly($primaryKey); - return preg_replace('/(' . $primaryKey . '|' . $studlyPrimaryKey . '| (_)'. $primaryKey.')$/', '', $foreignKey); + return preg_replace('/(' . $primaryKey . '|' . $studlyPrimaryKey . '|(_)'. $primaryKey.')$/', '', $foreignKey); } } }