From 7346ca538295017014090bc7230ca41070618320 Mon Sep 17 00:00:00 2001 From: Ade Reksi Susanto Date: Fri, 27 Oct 2023 16:53:27 +0700 Subject: [PATCH 1/3] Create new relation_name_strategy --- config/models.php | 39 ++++++++++----------- src/Coders/Model/Relations/BelongsTo.php | 35 +++++++++++------- src/Coders/Model/Relations/HasMany.php | 5 ++- src/Coders/Model/Relations/HasOneOrMany.php | 22 ++++++++---- 4 files changed, 61 insertions(+), 40 deletions(-) diff --git a/config/models.php b/config/models.php index c5b09cd9..9ea95dc0 100644 --- a/config/models.php +++ b/config/models.php @@ -341,9 +341,7 @@ | 'billing_invoices' => 'Invoice', */ - 'model_names' => [ - - ], + 'model_names' => [], /* |-------------------------------------------------------------------------- @@ -370,10 +368,13 @@ | 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() + | + | 'related_with_foreign_key_and_local_key' set foreign_key and set local_key in relation. */ 'relation_name_strategy' => 'related', // 'relation_name_strategy' => 'foreign_key', + // 'relation_name_strategy' => 'related_with_foreign_key_and_local_key', /* |-------------------------------------------------------------------------- @@ -413,9 +414,7 @@ | You can enable pluralization for certain tables | */ - 'override_pluralize_for' => [ - - ], + 'override_pluralize_for' => [], /* |-------------------------------------------------------------------------- | Move $fillable property to base files @@ -493,18 +492,18 @@ | */ -// 'connections' => [ -// 'read_only_external' => [ -// 'parent' => \App\Models\ReadOnlyModel::class, -// 'connection' => true, -// 'users' => [ -// 'connection' => false, -// ], -// 'my_other_database' => [ -// 'password_resets' => [ -// 'connection' => false, -// ] -// ] -// ], -// ], + // 'connections' => [ + // 'read_only_external' => [ + // 'parent' => \App\Models\ReadOnlyModel::class, + // 'connection' => true, + // 'users' => [ + // 'connection' => false, + // ], + // 'my_other_database' => [ + // 'password_resets' => [ + // 'connection' => false, + // ] + // ] + // ], + // ], ]; diff --git a/src/Coders/Model/Relations/BelongsTo.php b/src/Coders/Model/Relations/BelongsTo.php index dba8c345..5ac4cba4 100644 --- a/src/Coders/Model/Relations/BelongsTo.php +++ b/src/Coders/Model/Relations/BelongsTo.php @@ -50,6 +50,9 @@ public function __construct(Fluent $command, Model $parent, Model $related) public function name() { switch ($this->parent->getRelationNameStrategy()) { + case 'related_with_foreign_key_and_local_key': + $relationName = $this->related->getClassName(); + break; case 'foreign_key': $relationName = RelationHelper::stripSuffixFromForeignKey( $this->parent->usesSnakeAttributes(), @@ -77,20 +80,20 @@ public function body() { $body = 'return $this->belongsTo('; - $body .= $this->related->getQualifiedUserClassName().'::class'; + $body .= $this->related->getQualifiedUserClassName() . '::class'; if ($this->needsForeignKey()) { $foreignKey = $this->parent->usesPropertyConstants() - ? $this->parent->getQualifiedUserClassName().'::'.strtoupper($this->foreignKey()) + ? $this->parent->getQualifiedUserClassName() . '::' . strtoupper($this->foreignKey()) : $this->foreignKey(); - $body .= ', '.Dumper::export($foreignKey); + $body .= ', ' . Dumper::export($foreignKey); } if ($this->needsOtherKey()) { $otherKey = $this->related->usesPropertyConstants() - ? $this->related->getQualifiedUserClassName().'::'.strtoupper($this->otherKey()) + ? $this->related->getQualifiedUserClassName() . '::' . strtoupper($this->otherKey()) : $this->otherKey(); - $body .= ', '.Dumper::export($otherKey); + $body .= ', ' . Dumper::export($otherKey); } $body .= ')'; @@ -100,10 +103,10 @@ public function body() // or a composite unique key. Otherwise it should be a has-many relationship which is not // supported at the moment. @todo: Improve relationship resolution. foreach ($this->command->references as $index => $column) { - $body .= "\n\t\t\t\t\t->where(". - Dumper::export($this->qualifiedOtherKey($index)). - ", '=', ". - Dumper::export($this->qualifiedForeignKey($index)). + $body .= "\n\t\t\t\t\t->where(" . + Dumper::export($this->qualifiedOtherKey($index)) . + ", '=', " . + Dumper::export($this->qualifiedForeignKey($index)) . ')'; } } @@ -140,7 +143,11 @@ public function returnType() */ protected function needsForeignKey() { - $defaultForeignKey = $this->related->getRecordName().'_id'; + if ($this->parent->config('relation_name_strategy') === 'related_with_foreign_key_and_local_key') { + return true; + } + + $defaultForeignKey = $this->related->getRecordName() . '_id'; return $defaultForeignKey != $this->foreignKey() || $this->needsOtherKey(); } @@ -162,7 +169,7 @@ protected function foreignKey($index = 0) */ protected function qualifiedForeignKey($index = 0) { - return $this->parent->getTable().'.'.$this->foreignKey($index); + return $this->parent->getTable() . '.' . $this->foreignKey($index); } /** @@ -170,6 +177,10 @@ protected function qualifiedForeignKey($index = 0) */ protected function needsOtherKey() { + if ($this->parent->config('relation_name_strategy') === 'related_with_foreign_key_and_local_key') { + return true; + } + $defaultOtherKey = $this->related->getPrimaryKey(); return $defaultOtherKey != $this->otherKey(); @@ -192,7 +203,7 @@ protected function otherKey($index = 0) */ protected function qualifiedOtherKey($index = 0) { - return $this->related->getTable().'.'.$this->otherKey($index); + return $this->related->getTable() . '.' . $this->otherKey($index); } /** diff --git a/src/Coders/Model/Relations/HasMany.php b/src/Coders/Model/Relations/HasMany.php index 76c448a7..4c3f5396 100644 --- a/src/Coders/Model/Relations/HasMany.php +++ b/src/Coders/Model/Relations/HasMany.php @@ -17,7 +17,7 @@ class HasMany extends HasOneOrMany */ public function hint() { - return '\\'.Collection::class.'|'.$this->related->getQualifiedUserClassName().'[]'; + return '\\' . Collection::class . '|' . $this->related->getQualifiedUserClassName() . '[]'; } /** @@ -26,6 +26,9 @@ public function hint() public function name() { switch ($this->parent->getRelationNameStrategy()) { + case 'related_with_foreign_key_and_local_key': + $relationName = $this->related->getClassName(); + break; case 'foreign_key': $relationName = RelationHelper::stripSuffixFromForeignKey( $this->parent->usesSnakeAttributes(), diff --git a/src/Coders/Model/Relations/HasOneOrMany.php b/src/Coders/Model/Relations/HasOneOrMany.php index ae9e26e2..ace1ac8d 100644 --- a/src/Coders/Model/Relations/HasOneOrMany.php +++ b/src/Coders/Model/Relations/HasOneOrMany.php @@ -58,22 +58,22 @@ abstract public function name(); */ public function body() { - $body = 'return $this->'.$this->method().'('; + $body = 'return $this->' . $this->method() . '('; - $body .= $this->related->getQualifiedUserClassName().'::class'; + $body .= $this->related->getQualifiedUserClassName() . '::class'; if ($this->needsForeignKey()) { $foreignKey = $this->parent->usesPropertyConstants() - ? $this->related->getQualifiedUserClassName().'::'.strtoupper($this->foreignKey()) + ? $this->related->getQualifiedUserClassName() . '::' . strtoupper($this->foreignKey()) : $this->foreignKey(); - $body .= ', '.Dumper::export($foreignKey); + $body .= ', ' . Dumper::export($foreignKey); } if ($this->needsLocalKey()) { $localKey = $this->related->usesPropertyConstants() - ? $this->related->getQualifiedUserClassName().'::'.strtoupper($this->localKey()) + ? $this->related->getQualifiedUserClassName() . '::' . strtoupper($this->localKey()) : $this->localKey(); - $body .= ', '.Dumper::export($localKey); + $body .= ', ' . Dumper::export($localKey); } $body .= ');'; @@ -91,7 +91,11 @@ abstract protected function method(); */ protected function needsForeignKey() { - $defaultForeignKey = $this->parent->getRecordName().'_id'; + if ($this->parent->config('relation_name_strategy') === 'related_with_foreign_key_and_local_key') { + return true; + } + + $defaultForeignKey = $this->parent->getRecordName() . '_id'; return $defaultForeignKey != $this->foreignKey() || $this->needsLocalKey(); } @@ -109,6 +113,10 @@ protected function foreignKey() */ protected function needsLocalKey() { + if ($this->parent->config('relation_name_strategy') === 'related_with_foreign_key_and_local_key') { + return true; + } + return $this->parent->getPrimaryKey() != $this->localKey(); } From b680a8d50c61d8dd284c3aa57018e02202baf03a Mon Sep 17 00:00:00 2001 From: Ade Reksi Susanto <34746706+adereksisusanto@users.noreply.github.com> Date: Mon, 30 Oct 2023 12:14:34 +0700 Subject: [PATCH 2/3] Update Config Delete Config 'relation_name_strategy' => 'related_with_foreign_key_and_local_key', Create Config 'relation' => [ 'options' => [ /* | 'true' return $this->belongsTo(User::class, 'user_id', 'id'); (post.user_id --> user.id) | return $this->hasMany(Comment::class, 'post_id', 'id'); (comment.post_id --> post.id) | | 'false' return $this->belongsTo(User::class); (post.user_id --> user.id) | return $this->hasMany(Comment::class); (comment.post_id --> post.id) */ 'show_key' => false, // default: false ] ], --- config/models.php | 18 +++++++--- src/Coders/Model/Relations/BelongsTo.php | 9 ++--- src/Coders/Model/Relations/BelongsToMany.php | 35 ++++++++++++-------- src/Coders/Model/Relations/HasMany.php | 5 +-- src/Coders/Model/Relations/HasOne.php | 2 +- src/Coders/Model/Relations/HasOneOrMany.php | 6 ++-- 6 files changed, 44 insertions(+), 31 deletions(-) diff --git a/config/models.php b/config/models.php index 9ea95dc0..1e4371b6 100644 --- a/config/models.php +++ b/config/models.php @@ -368,13 +368,23 @@ | 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() - | - | 'related_with_foreign_key_and_local_key' set foreign_key and set local_key in relation. */ 'relation_name_strategy' => 'related', // 'relation_name_strategy' => 'foreign_key', - // 'relation_name_strategy' => 'related_with_foreign_key_and_local_key', + + 'relation' => [ + 'options' => [ + /* + | 'true' return $this->belongsTo(User::class, 'user_id', 'id'); (post.user_id --> user.id) + | return $this->hasMany(Comment::class, 'post_id', 'id'); (comment.post_id --> post.id) + | + | 'false' return $this->belongsTo(User::class); (post.user_id --> user.id) + | return $this->hasMany(Comment::class); (comment.post_id --> post.id) + */ + 'show_key' => false, // default: false + ] + ], /* |-------------------------------------------------------------------------- @@ -506,4 +516,4 @@ // ] // ], // ], -]; +]; \ No newline at end of file diff --git a/src/Coders/Model/Relations/BelongsTo.php b/src/Coders/Model/Relations/BelongsTo.php index 5ac4cba4..7754d3f0 100644 --- a/src/Coders/Model/Relations/BelongsTo.php +++ b/src/Coders/Model/Relations/BelongsTo.php @@ -50,9 +50,6 @@ public function __construct(Fluent $command, Model $parent, Model $related) public function name() { switch ($this->parent->getRelationNameStrategy()) { - case 'related_with_foreign_key_and_local_key': - $relationName = $this->related->getClassName(); - break; case 'foreign_key': $relationName = RelationHelper::stripSuffixFromForeignKey( $this->parent->usesSnakeAttributes(), @@ -143,7 +140,7 @@ public function returnType() */ protected function needsForeignKey() { - if ($this->parent->config('relation_name_strategy') === 'related_with_foreign_key_and_local_key') { + if ($this->parent->config('relation.options.show_key')) { return true; } @@ -177,7 +174,7 @@ protected function qualifiedForeignKey($index = 0) */ protected function needsOtherKey() { - if ($this->parent->config('relation_name_strategy') === 'related_with_foreign_key_and_local_key') { + if ($this->parent->config('relation.options.show_key')) { return true; } @@ -223,4 +220,4 @@ private function isNullable() { return (bool) $this->parent->getBlueprint()->column($this->foreignKey())->get('nullable'); } -} +} \ No newline at end of file diff --git a/src/Coders/Model/Relations/BelongsToMany.php b/src/Coders/Model/Relations/BelongsToMany.php index c528f5e2..3564d509 100644 --- a/src/Coders/Model/Relations/BelongsToMany.php +++ b/src/Coders/Model/Relations/BelongsToMany.php @@ -69,7 +69,7 @@ public function __construct( */ public function hint() { - return '\\'.Collection::class.'|'.$this->reference->getQualifiedUserClassName().'[]'; + return '\\' . Collection::class . '|' . $this->reference->getQualifiedUserClassName() . '[]'; } /** @@ -99,32 +99,32 @@ public function body() { $body = 'return $this->belongsToMany('; - $body .= $this->reference->getQualifiedUserClassName().'::class'; + $body .= $this->reference->getQualifiedUserClassName() . '::class'; if ($this->needsPivotTable()) { - $body .= ', '.Dumper::export($this->pivotTable()); + $body .= ', ' . Dumper::export($this->pivotTable()); } if ($this->needsForeignKey()) { $foreignKey = $this->parent->usesPropertyConstants() - ? $this->reference->getQualifiedUserClassName().'::'.strtoupper($this->foreignKey()) + ? $this->reference->getQualifiedUserClassName() . '::' . strtoupper($this->foreignKey()) : $this->foreignKey(); - $body .= ', '.Dumper::export($foreignKey); + $body .= ', ' . Dumper::export($foreignKey); } if ($this->needsOtherKey()) { $otherKey = $this->reference->usesPropertyConstants() - ? $this->reference->getQualifiedUserClassName().'::'.strtoupper($this->otherKey()) + ? $this->reference->getQualifiedUserClassName() . '::' . strtoupper($this->otherKey()) : $this->otherKey(); - $body .= ', '.Dumper::export($otherKey); + $body .= ', ' . Dumper::export($otherKey); } $body .= ')'; $fields = $this->getPivotFields(); - if (! empty($fields)) { - $body .= "\n\t\t\t\t\t->withPivot(".$this->parametrize($fields).')'; + if (!empty($fields)) { + $body .= "\n\t\t\t\t\t->withPivot(" . $this->parametrize($fields) . ')'; } if ($this->pivot->usesTimestamps()) { @@ -173,7 +173,11 @@ protected function pivotTable() */ protected function needsForeignKey() { - $defaultForeignKey = $this->parentRecordName().'_id'; + if ($this->parent->config('relation.options.show_key')) { + return true; + } + + $defaultForeignKey = $this->parentRecordName() . '_id'; return $this->foreignKey() != $defaultForeignKey || $this->needsOtherKey(); } @@ -191,7 +195,12 @@ protected function foreignKey() */ protected function needsOtherKey() { - $defaultOtherKey = $this->referenceRecordName().'_id'; + + if ($this->parent->config('relation.options.show_key')) { + return true; + } + + $defaultOtherKey = $this->referenceRecordName() . '_id'; return $this->otherKey() != $defaultOtherKey; } @@ -241,10 +250,10 @@ private function parametrize($fields = []) { return (string) implode(', ', array_map(function ($field) { $field = $this->reference->usesPropertyConstants() - ? $this->pivot->getQualifiedUserClassName().'::'.strtoupper($field) + ? $this->pivot->getQualifiedUserClassName() . '::' . strtoupper($field) : $field; return Dumper::export($field); }, $fields)); } -} +} \ No newline at end of file diff --git a/src/Coders/Model/Relations/HasMany.php b/src/Coders/Model/Relations/HasMany.php index 4c3f5396..e6b2deb6 100644 --- a/src/Coders/Model/Relations/HasMany.php +++ b/src/Coders/Model/Relations/HasMany.php @@ -26,9 +26,6 @@ public function hint() public function name() { switch ($this->parent->getRelationNameStrategy()) { - case 'related_with_foreign_key_and_local_key': - $relationName = $this->related->getClassName(); - break; case 'foreign_key': $relationName = RelationHelper::stripSuffixFromForeignKey( $this->parent->usesSnakeAttributes(), @@ -69,4 +66,4 @@ public function returnType() { return \Illuminate\Database\Eloquent\Relations\HasMany::class; } -} +} \ No newline at end of file diff --git a/src/Coders/Model/Relations/HasOne.php b/src/Coders/Model/Relations/HasOne.php index 3381a853..2e08dbe4 100644 --- a/src/Coders/Model/Relations/HasOne.php +++ b/src/Coders/Model/Relations/HasOne.php @@ -46,4 +46,4 @@ public function returnType() { return \Illuminate\Database\Eloquent\Relations\HasOne::class; } -} +} \ No newline at end of file diff --git a/src/Coders/Model/Relations/HasOneOrMany.php b/src/Coders/Model/Relations/HasOneOrMany.php index ace1ac8d..4b3af8f0 100644 --- a/src/Coders/Model/Relations/HasOneOrMany.php +++ b/src/Coders/Model/Relations/HasOneOrMany.php @@ -91,7 +91,7 @@ abstract protected function method(); */ protected function needsForeignKey() { - if ($this->parent->config('relation_name_strategy') === 'related_with_foreign_key_and_local_key') { + if ($this->parent->config('relation.options.show_key')) { return true; } @@ -113,7 +113,7 @@ protected function foreignKey() */ protected function needsLocalKey() { - if ($this->parent->config('relation_name_strategy') === 'related_with_foreign_key_and_local_key') { + if ($this->parent->config('relation.options.show_key')) { return true; } @@ -127,4 +127,4 @@ protected function localKey() { return $this->command->references[0]; } -} +} \ No newline at end of file From c66f2cb9bc8942866748a87e408557649b02a9ac Mon Sep 17 00:00:00 2001 From: Ade Reksi Susanto Date: Tue, 31 Oct 2023 00:12:27 +0700 Subject: [PATCH 3/3] Fix Bugs: BelongsToMany --- src/Coders/Model/Relations/BelongsToMany.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/Coders/Model/Relations/BelongsToMany.php b/src/Coders/Model/Relations/BelongsToMany.php index 3564d509..ec92b0cb 100644 --- a/src/Coders/Model/Relations/BelongsToMany.php +++ b/src/Coders/Model/Relations/BelongsToMany.php @@ -173,10 +173,6 @@ protected function pivotTable() */ protected function needsForeignKey() { - if ($this->parent->config('relation.options.show_key')) { - return true; - } - $defaultForeignKey = $this->parentRecordName() . '_id'; return $this->foreignKey() != $defaultForeignKey || $this->needsOtherKey(); @@ -195,11 +191,6 @@ protected function foreignKey() */ protected function needsOtherKey() { - - if ($this->parent->config('relation.options.show_key')) { - return true; - } - $defaultOtherKey = $this->referenceRecordName() . '_id'; return $this->otherKey() != $defaultOtherKey; @@ -256,4 +247,4 @@ private function parametrize($fields = []) return Dumper::export($field); }, $fields)); } -} \ No newline at end of file +}