Skip to content

Commit 759eac3

Browse files
authored
Fix relation name infering when MorphOne/MorphMany (#473)
1 parent 17df0e3 commit 759eac3

File tree

4 files changed

+92
-4
lines changed

4 files changed

+92
-4
lines changed

src/Generators/ModelGenerator.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,15 @@ protected function buildRelationships(Model $model)
202202
if (Str::contains($reference, ':')) {
203203
[$foreign_reference, $column_name] = explode(':', $reference);
204204

205-
$method_name = $is_model_fqn ? Str::afterLast($foreign_reference, '\\') : Str::beforeLast($column_name, '_id');
205+
$method_name = Str::beforeLast($column_name, '_id');
206206

207207
if (Str::contains($foreign_reference, '.')) {
208208
[$class, $key] = explode('.', $foreign_reference);
209209

210210
if ($key === 'id') {
211211
$key = null;
212-
} else {
213-
$method_name = $is_model_fqn ? Str::lower(Str::afterLast($class, '\\')) : Str::lower($class);
214212
}
213+
$method_name = $is_model_fqn ? Str::lower(Str::afterLast($class, '\\')) : Str::lower($class);
215214
} else {
216215
$class = $foreign_reference;
217216
}
@@ -230,7 +229,7 @@ protected function buildRelationships(Model $model)
230229
if ($type === 'morphTo') {
231230
$relationship = sprintf('$this->%s()', $type);
232231
} elseif ($type === 'morphMany' || $type === 'morphOne') {
233-
$relation = Str::lower(Str::singular($column_name)) . 'able';
232+
$relation = Str::lower($is_model_fqn ? Str::singular(Str::afterLast($column_name, '\\')) : Str::singular($column_name)) . 'able';
234233
$relationship = sprintf('$this->%s(%s::class, \'%s\')', $type, $fqcn, $relation);
235234
} elseif (!is_null($key)) {
236235
$relationship = sprintf('$this->%s(%s::class, \'%s\', \'%s\')', $type, $fqcn, $column_name, $key);

tests/Feature/Generators/ModelGeneratorTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,38 @@ public function output_generates_relationships_added_with_full_model_namespace()
300300
$this->assertEquals(['created' => ['app/Recurrency.php']], $this->subject->output($tree));
301301
}
302302

303+
/**
304+
* @test
305+
* @environment-setup useLaravel8
306+
*/
307+
public function output_generates_morphone_morphmany_relation_string_when_using_fqn()
308+
{
309+
$this->files->expects('stub')
310+
->with($this->modelStub)
311+
->andReturn($this->stub($this->modelStub));
312+
$this->files->expects('stub')
313+
->with('model.fillable.stub')
314+
->andReturn($this->stub('model.fillable.stub'));
315+
$this->files->expects('stub')
316+
->with('model.casts.stub')
317+
->andReturn($this->stub('model.casts.stub'));
318+
$this->files->expects('stub')
319+
->with('model.method.stub')
320+
->andReturn($this->stub('model.method.stub'));
321+
322+
$this->files->expects('exists')
323+
->with('app')
324+
->andReturnTrue();
325+
326+
$this->files->expects('put')
327+
->with('app/Flag.php', $this->fixture('models/model-relationships-morphone-morphmany-with-fqn-laravel8.php'));
328+
329+
$tokens = $this->blueprint->parse($this->fixture('drafts/model-relationships-morphone-morphmany-with-fqn.yaml'));
330+
$tree = $this->blueprint->analyze($tokens);
331+
332+
$this->assertEquals(['created' => ['app/Flag.php']], $this->subject->output($tree));
333+
}
334+
303335
/**
304336
* @test
305337
* @environment-setup useLaravel8
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
models:
2+
Flag:
3+
user_id: id
4+
relationships:
5+
morphOne: \Other\Package\Order:stars
6+
morphMany: \Other\Package\Duration, \App\MyCustom\Folder\Transaction:line
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Flag extends Model
9+
{
10+
use HasFactory;
11+
12+
/**
13+
* The attributes that are mass assignable.
14+
*
15+
* @var array
16+
*/
17+
protected $fillable = [
18+
'user_id',
19+
];
20+
21+
/**
22+
* The attributes that should be cast to native types.
23+
*
24+
* @var array
25+
*/
26+
protected $casts = [
27+
'id' => 'integer',
28+
'user_id' => 'integer',
29+
];
30+
31+
32+
public function stars()
33+
{
34+
return $this->morphOne(\Other\Package\Order::class, 'starable');
35+
}
36+
37+
public function durations()
38+
{
39+
return $this->morphMany(\Other\Package\Duration::class, 'durationable');
40+
}
41+
42+
public function lines()
43+
{
44+
return $this->morphMany(\App\MyCustom\Folder\Transaction::class, 'lineable');
45+
}
46+
47+
public function user()
48+
{
49+
return $this->belongsTo(\App\User::class);
50+
}
51+
}

0 commit comments

Comments
 (0)