Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new option in relation_name_strategy key to name has many Relations with plural(Field_name) #194

Open
wants to merge 9 commits into
base: v1.x
Choose a base branch
from
19 changes: 17 additions & 2 deletions config/models.php
Expand Up @@ -346,10 +346,25 @@
| 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',

/*
|--------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions src/Coders/Model/Relations/BelongsTo.php
Expand Up @@ -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(),
Expand Down
12 changes: 12 additions & 0 deletions src/Coders/Model/Relations/HasMany.php
Expand Up @@ -26,6 +26,18 @@ public function hint()
public function name()
{
switch ($this->parent->getRelationNameStrategy()) {
case 'foreign_key_field_name':
$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 = ucfirst(Str::singular($relationName)).Str::plural($this->related->getClassName());
}
break;
case 'foreign_key':
$relationName = RelationHelper::stripSuffixFromForeignKey(
$this->parent->usesSnakeAttributes(),
Expand Down
2 changes: 1 addition & 1 deletion src/Coders/Model/Relations/RelationHelper.php
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as i mentioned before this change fixes the User::comments() relation example cause in the HasMany Relation class we have this condition "if (Str::snake($relationName) === Str::snake($this->parent->getClassName()))" to check the foreign key name
before my change it returns "user_ === user" because the old code does not replace the "_" from the foreign key , we need to replace _id or Id from the foreign key to generate the relation name with plural of the other table (comments not userComments)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying, @khattab17. It does break some of our tests though. Which means it is introducing some unexpected behaviour as well. If you run phpunit, you might be able to see what's going on.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i fixed this bug , i think now it passes the phpunit test

}
}
}