Skip to content

Commit 7346ca5

Browse files
Create new relation_name_strategy
1 parent 3e0d2e5 commit 7346ca5

File tree

4 files changed

+61
-40
lines changed

4 files changed

+61
-40
lines changed

config/models.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,7 @@
341341
| 'billing_invoices' => 'Invoice',
342342
*/
343343

344-
'model_names' => [
345-
346-
],
344+
'model_names' => [],
347345

348346
/*
349347
|--------------------------------------------------------------------------
@@ -370,10 +368,13 @@
370368
| Where the foreign key matches the related table name, it behaves as per the 'related' strategy.
371369
| (post.user_id --> user.id)
372370
| generates Post::user() and User::posts()
371+
|
372+
| 'related_with_foreign_key_and_local_key' set foreign_key and set local_key in relation.
373373
*/
374374

375375
'relation_name_strategy' => 'related',
376376
// 'relation_name_strategy' => 'foreign_key',
377+
// 'relation_name_strategy' => 'related_with_foreign_key_and_local_key',
377378

378379
/*
379380
|--------------------------------------------------------------------------
@@ -413,9 +414,7 @@
413414
| You can enable pluralization for certain tables
414415
|
415416
*/
416-
'override_pluralize_for' => [
417-
418-
],
417+
'override_pluralize_for' => [],
419418
/*
420419
|--------------------------------------------------------------------------
421420
| Move $fillable property to base files
@@ -493,18 +492,18 @@
493492
|
494493
*/
495494

496-
// 'connections' => [
497-
// 'read_only_external' => [
498-
// 'parent' => \App\Models\ReadOnlyModel::class,
499-
// 'connection' => true,
500-
// 'users' => [
501-
// 'connection' => false,
502-
// ],
503-
// 'my_other_database' => [
504-
// 'password_resets' => [
505-
// 'connection' => false,
506-
// ]
507-
// ]
508-
// ],
509-
// ],
495+
// 'connections' => [
496+
// 'read_only_external' => [
497+
// 'parent' => \App\Models\ReadOnlyModel::class,
498+
// 'connection' => true,
499+
// 'users' => [
500+
// 'connection' => false,
501+
// ],
502+
// 'my_other_database' => [
503+
// 'password_resets' => [
504+
// 'connection' => false,
505+
// ]
506+
// ]
507+
// ],
508+
// ],
510509
];

src/Coders/Model/Relations/BelongsTo.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ 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;
5356
case 'foreign_key':
5457
$relationName = RelationHelper::stripSuffixFromForeignKey(
5558
$this->parent->usesSnakeAttributes(),
@@ -77,20 +80,20 @@ public function body()
7780
{
7881
$body = 'return $this->belongsTo(';
7982

80-
$body .= $this->related->getQualifiedUserClassName().'::class';
83+
$body .= $this->related->getQualifiedUserClassName() . '::class';
8184

8285
if ($this->needsForeignKey()) {
8386
$foreignKey = $this->parent->usesPropertyConstants()
84-
? $this->parent->getQualifiedUserClassName().'::'.strtoupper($this->foreignKey())
87+
? $this->parent->getQualifiedUserClassName() . '::' . strtoupper($this->foreignKey())
8588
: $this->foreignKey();
86-
$body .= ', '.Dumper::export($foreignKey);
89+
$body .= ', ' . Dumper::export($foreignKey);
8790
}
8891

8992
if ($this->needsOtherKey()) {
9093
$otherKey = $this->related->usesPropertyConstants()
91-
? $this->related->getQualifiedUserClassName().'::'.strtoupper($this->otherKey())
94+
? $this->related->getQualifiedUserClassName() . '::' . strtoupper($this->otherKey())
9295
: $this->otherKey();
93-
$body .= ', '.Dumper::export($otherKey);
96+
$body .= ', ' . Dumper::export($otherKey);
9497
}
9598

9699
$body .= ')';
@@ -100,10 +103,10 @@ public function body()
100103
// or a composite unique key. Otherwise it should be a has-many relationship which is not
101104
// supported at the moment. @todo: Improve relationship resolution.
102105
foreach ($this->command->references as $index => $column) {
103-
$body .= "\n\t\t\t\t\t->where(".
104-
Dumper::export($this->qualifiedOtherKey($index)).
105-
", '=', ".
106-
Dumper::export($this->qualifiedForeignKey($index)).
106+
$body .= "\n\t\t\t\t\t->where(" .
107+
Dumper::export($this->qualifiedOtherKey($index)) .
108+
", '=', " .
109+
Dumper::export($this->qualifiedForeignKey($index)) .
107110
')';
108111
}
109112
}
@@ -140,7 +143,11 @@ public function returnType()
140143
*/
141144
protected function needsForeignKey()
142145
{
143-
$defaultForeignKey = $this->related->getRecordName().'_id';
146+
if ($this->parent->config('relation_name_strategy') === 'related_with_foreign_key_and_local_key') {
147+
return true;
148+
}
149+
150+
$defaultForeignKey = $this->related->getRecordName() . '_id';
144151

145152
return $defaultForeignKey != $this->foreignKey() || $this->needsOtherKey();
146153
}
@@ -162,14 +169,18 @@ protected function foreignKey($index = 0)
162169
*/
163170
protected function qualifiedForeignKey($index = 0)
164171
{
165-
return $this->parent->getTable().'.'.$this->foreignKey($index);
172+
return $this->parent->getTable() . '.' . $this->foreignKey($index);
166173
}
167174

168175
/**
169176
* @return bool
170177
*/
171178
protected function needsOtherKey()
172179
{
180+
if ($this->parent->config('relation_name_strategy') === 'related_with_foreign_key_and_local_key') {
181+
return true;
182+
}
183+
173184
$defaultOtherKey = $this->related->getPrimaryKey();
174185

175186
return $defaultOtherKey != $this->otherKey();
@@ -192,7 +203,7 @@ protected function otherKey($index = 0)
192203
*/
193204
protected function qualifiedOtherKey($index = 0)
194205
{
195-
return $this->related->getTable().'.'.$this->otherKey($index);
206+
return $this->related->getTable() . '.' . $this->otherKey($index);
196207
}
197208

198209
/**

src/Coders/Model/Relations/HasMany.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class HasMany extends HasOneOrMany
1717
*/
1818
public function hint()
1919
{
20-
return '\\'.Collection::class.'|'.$this->related->getQualifiedUserClassName().'[]';
20+
return '\\' . Collection::class . '|' . $this->related->getQualifiedUserClassName() . '[]';
2121
}
2222

2323
/**
@@ -26,6 +26,9 @@ 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;
2932
case 'foreign_key':
3033
$relationName = RelationHelper::stripSuffixFromForeignKey(
3134
$this->parent->usesSnakeAttributes(),

src/Coders/Model/Relations/HasOneOrMany.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,22 @@ abstract public function name();
5858
*/
5959
public function body()
6060
{
61-
$body = 'return $this->'.$this->method().'(';
61+
$body = 'return $this->' . $this->method() . '(';
6262

63-
$body .= $this->related->getQualifiedUserClassName().'::class';
63+
$body .= $this->related->getQualifiedUserClassName() . '::class';
6464

6565
if ($this->needsForeignKey()) {
6666
$foreignKey = $this->parent->usesPropertyConstants()
67-
? $this->related->getQualifiedUserClassName().'::'.strtoupper($this->foreignKey())
67+
? $this->related->getQualifiedUserClassName() . '::' . strtoupper($this->foreignKey())
6868
: $this->foreignKey();
69-
$body .= ', '.Dumper::export($foreignKey);
69+
$body .= ', ' . Dumper::export($foreignKey);
7070
}
7171

7272
if ($this->needsLocalKey()) {
7373
$localKey = $this->related->usesPropertyConstants()
74-
? $this->related->getQualifiedUserClassName().'::'.strtoupper($this->localKey())
74+
? $this->related->getQualifiedUserClassName() . '::' . strtoupper($this->localKey())
7575
: $this->localKey();
76-
$body .= ', '.Dumper::export($localKey);
76+
$body .= ', ' . Dumper::export($localKey);
7777
}
7878

7979
$body .= ');';
@@ -91,7 +91,11 @@ abstract protected function method();
9191
*/
9292
protected function needsForeignKey()
9393
{
94-
$defaultForeignKey = $this->parent->getRecordName().'_id';
94+
if ($this->parent->config('relation_name_strategy') === 'related_with_foreign_key_and_local_key') {
95+
return true;
96+
}
97+
98+
$defaultForeignKey = $this->parent->getRecordName() . '_id';
9599

96100
return $defaultForeignKey != $this->foreignKey() || $this->needsLocalKey();
97101
}
@@ -109,6 +113,10 @@ protected function foreignKey()
109113
*/
110114
protected function needsLocalKey()
111115
{
116+
if ($this->parent->config('relation_name_strategy') === 'related_with_foreign_key_and_local_key') {
117+
return true;
118+
}
119+
112120
return $this->parent->getPrimaryKey() != $this->localKey();
113121
}
114122

0 commit comments

Comments
 (0)