Skip to content

Commit 308fa78

Browse files
Remove "related_with_foreign_key_and_local_key"
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 ] ],
1 parent 7346ca5 commit 308fa78

File tree

6 files changed

+44
-31
lines changed

6 files changed

+44
-31
lines changed

config/models.php

+14-4
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,23 @@
368368
| Where the foreign key matches the related table name, it behaves as per the 'related' strategy.
369369
| (post.user_id --> user.id)
370370
| generates Post::user() and User::posts()
371-
|
372-
| 'related_with_foreign_key_and_local_key' set foreign_key and set local_key in relation.
373371
*/
374372

375373
'relation_name_strategy' => 'related',
376374
// 'relation_name_strategy' => 'foreign_key',
377-
// 'relation_name_strategy' => 'related_with_foreign_key_and_local_key',
375+
376+
'relation' => [
377+
'options' => [
378+
/*
379+
| 'true' return $this->belongsTo(User::class, 'user_id', 'id'); (post.user_id --> user.id)
380+
| return $this->hasMany(Comment::class, 'post_id', 'id'); (comment.post_id --> post.id)
381+
|
382+
| 'false' return $this->belongsTo(User::class); (post.user_id --> user.id)
383+
| return $this->hasMany(Comment::class); (comment.post_id --> post.id)
384+
*/
385+
'show_key' => false, // default: false
386+
]
387+
],
378388

379389
/*
380390
|--------------------------------------------------------------------------
@@ -506,4 +516,4 @@
506516
// ]
507517
// ],
508518
// ],
509-
];
519+
];

src/Coders/Model/Relations/BelongsTo.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ public function __construct(Fluent $command, Model $parent, Model $related)
5050
public function name()
5151
{
5252
switch ($this->parent->getRelationNameStrategy()) {
53-
case 'related_with_foreign_key_and_local_key':
54-
$relationName = $this->related->getClassName();
55-
break;
5653
case 'foreign_key':
5754
$relationName = RelationHelper::stripSuffixFromForeignKey(
5855
$this->parent->usesSnakeAttributes(),
@@ -143,7 +140,7 @@ public function returnType()
143140
*/
144141
protected function needsForeignKey()
145142
{
146-
if ($this->parent->config('relation_name_strategy') === 'related_with_foreign_key_and_local_key') {
143+
if ($this->parent->config('relation.options.show_key')) {
147144
return true;
148145
}
149146

@@ -177,7 +174,7 @@ protected function qualifiedForeignKey($index = 0)
177174
*/
178175
protected function needsOtherKey()
179176
{
180-
if ($this->parent->config('relation_name_strategy') === 'related_with_foreign_key_and_local_key') {
177+
if ($this->parent->config('relation.options.show_key')) {
181178
return true;
182179
}
183180

@@ -223,4 +220,4 @@ private function isNullable()
223220
{
224221
return (bool) $this->parent->getBlueprint()->column($this->foreignKey())->get('nullable');
225222
}
226-
}
223+
}

src/Coders/Model/Relations/BelongsToMany.php

+22-13
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function __construct(
6969
*/
7070
public function hint()
7171
{
72-
return '\\'.Collection::class.'|'.$this->reference->getQualifiedUserClassName().'[]';
72+
return '\\' . Collection::class . '|' . $this->reference->getQualifiedUserClassName() . '[]';
7373
}
7474

7575
/**
@@ -99,32 +99,32 @@ public function body()
9999
{
100100
$body = 'return $this->belongsToMany(';
101101

102-
$body .= $this->reference->getQualifiedUserClassName().'::class';
102+
$body .= $this->reference->getQualifiedUserClassName() . '::class';
103103

104104
if ($this->needsPivotTable()) {
105-
$body .= ', '.Dumper::export($this->pivotTable());
105+
$body .= ', ' . Dumper::export($this->pivotTable());
106106
}
107107

108108
if ($this->needsForeignKey()) {
109109
$foreignKey = $this->parent->usesPropertyConstants()
110-
? $this->reference->getQualifiedUserClassName().'::'.strtoupper($this->foreignKey())
110+
? $this->reference->getQualifiedUserClassName() . '::' . strtoupper($this->foreignKey())
111111
: $this->foreignKey();
112-
$body .= ', '.Dumper::export($foreignKey);
112+
$body .= ', ' . Dumper::export($foreignKey);
113113
}
114114

115115
if ($this->needsOtherKey()) {
116116
$otherKey = $this->reference->usesPropertyConstants()
117-
? $this->reference->getQualifiedUserClassName().'::'.strtoupper($this->otherKey())
117+
? $this->reference->getQualifiedUserClassName() . '::' . strtoupper($this->otherKey())
118118
: $this->otherKey();
119-
$body .= ', '.Dumper::export($otherKey);
119+
$body .= ', ' . Dumper::export($otherKey);
120120
}
121121

122122
$body .= ')';
123123

124124
$fields = $this->getPivotFields();
125125

126-
if (! empty($fields)) {
127-
$body .= "\n\t\t\t\t\t->withPivot(".$this->parametrize($fields).')';
126+
if (!empty($fields)) {
127+
$body .= "\n\t\t\t\t\t->withPivot(" . $this->parametrize($fields) . ')';
128128
}
129129

130130
if ($this->pivot->usesTimestamps()) {
@@ -173,7 +173,11 @@ protected function pivotTable()
173173
*/
174174
protected function needsForeignKey()
175175
{
176-
$defaultForeignKey = $this->parentRecordName().'_id';
176+
if ($this->parent->config('relation.options.show_key')) {
177+
return true;
178+
}
179+
180+
$defaultForeignKey = $this->parentRecordName() . '_id';
177181

178182
return $this->foreignKey() != $defaultForeignKey || $this->needsOtherKey();
179183
}
@@ -191,7 +195,12 @@ protected function foreignKey()
191195
*/
192196
protected function needsOtherKey()
193197
{
194-
$defaultOtherKey = $this->referenceRecordName().'_id';
198+
199+
if ($this->parent->config('relation.options.show_key')) {
200+
return true;
201+
}
202+
203+
$defaultOtherKey = $this->referenceRecordName() . '_id';
195204

196205
return $this->otherKey() != $defaultOtherKey;
197206
}
@@ -241,10 +250,10 @@ private function parametrize($fields = [])
241250
{
242251
return (string) implode(', ', array_map(function ($field) {
243252
$field = $this->reference->usesPropertyConstants()
244-
? $this->pivot->getQualifiedUserClassName().'::'.strtoupper($field)
253+
? $this->pivot->getQualifiedUserClassName() . '::' . strtoupper($field)
245254
: $field;
246255

247256
return Dumper::export($field);
248257
}, $fields));
249258
}
250-
}
259+
}

src/Coders/Model/Relations/HasMany.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ public function hint()
2626
public function name()
2727
{
2828
switch ($this->parent->getRelationNameStrategy()) {
29-
case 'related_with_foreign_key_and_local_key':
30-
$relationName = $this->related->getClassName();
31-
break;
3229
case 'foreign_key':
3330
$relationName = RelationHelper::stripSuffixFromForeignKey(
3431
$this->parent->usesSnakeAttributes(),
@@ -69,4 +66,4 @@ public function returnType()
6966
{
7067
return \Illuminate\Database\Eloquent\Relations\HasMany::class;
7168
}
72-
}
69+
}

src/Coders/Model/Relations/HasOne.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ public function returnType()
4646
{
4747
return \Illuminate\Database\Eloquent\Relations\HasOne::class;
4848
}
49-
}
49+
}

src/Coders/Model/Relations/HasOneOrMany.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ abstract protected function method();
9191
*/
9292
protected function needsForeignKey()
9393
{
94-
if ($this->parent->config('relation_name_strategy') === 'related_with_foreign_key_and_local_key') {
94+
if ($this->parent->config('relation.options.show_key')) {
9595
return true;
9696
}
9797

@@ -113,7 +113,7 @@ protected function foreignKey()
113113
*/
114114
protected function needsLocalKey()
115115
{
116-
if ($this->parent->config('relation_name_strategy') === 'related_with_foreign_key_and_local_key') {
116+
if ($this->parent->config('relation.options.show_key')) {
117117
return true;
118118
}
119119

@@ -127,4 +127,4 @@ protected function localKey()
127127
{
128128
return $this->command->references[0];
129129
}
130-
}
130+
}

0 commit comments

Comments
 (0)