Skip to content

Commit

Permalink
Update Config
Browse files Browse the repository at this point in the history
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
    ]
],
  • Loading branch information
adereksisusanto committed Oct 30, 2023
1 parent 7346ca5 commit b680a8d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 31 deletions.
18 changes: 14 additions & 4 deletions config/models.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
],

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -506,4 +516,4 @@
// ]
// ],
// ],
];
];
9 changes: 3 additions & 6 deletions src/Coders/Model/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -223,4 +220,4 @@ private function isNullable()
{
return (bool) $this->parent->getBlueprint()->column($this->foreignKey())->get('nullable');
}
}
}
35 changes: 22 additions & 13 deletions src/Coders/Model/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function __construct(
*/
public function hint()
{
return '\\'.Collection::class.'|'.$this->reference->getQualifiedUserClassName().'[]';
return '\\' . Collection::class . '|' . $this->reference->getQualifiedUserClassName() . '[]';
}

/**
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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();
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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));
}
}
}
5 changes: 1 addition & 4 deletions src/Coders/Model/Relations/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -69,4 +66,4 @@ public function returnType()
{
return \Illuminate\Database\Eloquent\Relations\HasMany::class;
}
}
}
2 changes: 1 addition & 1 deletion src/Coders/Model/Relations/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ public function returnType()
{
return \Illuminate\Database\Eloquent\Relations\HasOne::class;
}
}
}
6 changes: 3 additions & 3 deletions src/Coders/Model/Relations/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -127,4 +127,4 @@ protected function localKey()
{
return $this->command->references[0];
}
}
}

0 comments on commit b680a8d

Please sign in to comment.