diff --git a/config/models.php b/config/models.php index c5b09cd9..1e4371b6 100644 --- a/config/models.php +++ b/config/models.php @@ -341,9 +341,7 @@ | 'billing_invoices' => 'Invoice', */ - 'model_names' => [ - - ], + 'model_names' => [], /* |-------------------------------------------------------------------------- @@ -375,6 +373,19 @@ 'relation_name_strategy' => 'related', // 'relation_name_strategy' => 'foreign_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 + ] + ], + /* |-------------------------------------------------------------------------- | Determines need or not to generate constants with properties names like @@ -413,9 +424,7 @@ | You can enable pluralization for certain tables | */ - 'override_pluralize_for' => [ - - ], + 'override_pluralize_for' => [], /* |-------------------------------------------------------------------------- | Move $fillable property to base files @@ -493,18 +502,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, + // ] + // ] + // ], + // ], +]; \ No newline at end of file diff --git a/src/Coders/Model/Relations/BelongsTo.php b/src/Coders/Model/Relations/BelongsTo.php index dba8c345..7754d3f0 100644 --- a/src/Coders/Model/Relations/BelongsTo.php +++ b/src/Coders/Model/Relations/BelongsTo.php @@ -77,20 +77,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 +100,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 +140,11 @@ public function returnType() */ protected function needsForeignKey() { - $defaultForeignKey = $this->related->getRecordName().'_id'; + if ($this->parent->config('relation.options.show_key')) { + return true; + } + + $defaultForeignKey = $this->related->getRecordName() . '_id'; return $defaultForeignKey != $this->foreignKey() || $this->needsOtherKey(); } @@ -162,7 +166,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 +174,10 @@ protected function qualifiedForeignKey($index = 0) */ protected function needsOtherKey() { + if ($this->parent->config('relation.options.show_key')) { + return true; + } + $defaultOtherKey = $this->related->getPrimaryKey(); return $defaultOtherKey != $this->otherKey(); @@ -192,7 +200,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); } /** @@ -212,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..ec92b0cb 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,7 @@ protected function pivotTable() */ protected function needsForeignKey() { - $defaultForeignKey = $this->parentRecordName().'_id'; + $defaultForeignKey = $this->parentRecordName() . '_id'; return $this->foreignKey() != $defaultForeignKey || $this->needsOtherKey(); } @@ -191,7 +191,7 @@ protected function foreignKey() */ protected function needsOtherKey() { - $defaultOtherKey = $this->referenceRecordName().'_id'; + $defaultOtherKey = $this->referenceRecordName() . '_id'; return $this->otherKey() != $defaultOtherKey; } @@ -241,7 +241,7 @@ 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); diff --git a/src/Coders/Model/Relations/HasMany.php b/src/Coders/Model/Relations/HasMany.php index 76c448a7..e6b2deb6 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() . '[]'; } /** @@ -66,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 ae9e26e2..4b3af8f0 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.options.show_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.options.show_key')) { + return true; + } + return $this->parent->getPrimaryKey() != $this->localKey(); } @@ -119,4 +127,4 @@ protected function localKey() { return $this->command->references[0]; } -} +} \ No newline at end of file