Skip to content

Commit 517ace1

Browse files
committed
Dont lookup belongstomany keys
1 parent 53c2c8c commit 517ace1

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

src/RelationFinder.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,6 @@ protected function getRelationshipFromMethodAndModel(ReflectionMethod $method, s
7272
$localKey = $return->getForeignKey();
7373
}
7474

75-
if ($return instanceof BelongsToMany && ! $return instanceof MorphToMany) {
76-
$foreignKey = $this->getParentKey($return->getQualifiedOwnerKeyName());
77-
$localKey = $return->getForeignKey();
78-
}
79-
8075
return [
8176
$method->getName() => new ModelRelation(
8277
$method->getShortName(),

tests/GetModelRelationsTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
namespace BeyondCode\ErdGenerator\Tests;
44

5-
use BeyondCode\ErdGenerator\GraphBuilder;
65
use BeyondCode\ErdGenerator\ModelRelation;
76
use BeyondCode\ErdGenerator\RelationFinder;
87
use BeyondCode\ErdGenerator\Tests\Models\Post;
98
use BeyondCode\ErdGenerator\Tests\Models\User;
109
use BeyondCode\ErdGenerator\Tests\Models\Avatar;
11-
use BeyondCode\ErdGenerator\GenerateDiagramCommand;
10+
use BeyondCode\ErdGenerator\Tests\Models\Comment;
1211

1312
class GetModelRelationsTest extends TestCase
1413
{
@@ -20,6 +19,8 @@ public function it_can_find_model_relations()
2019

2120
$relations = $finder->getModelRelations(User::class);
2221

22+
$this->assertCount(3, $relations);
23+
2324
$posts = $relations['posts'];
2425

2526
$this->assertInstanceOf(ModelRelation::class, $posts);
@@ -37,5 +38,14 @@ public function it_can_find_model_relations()
3738
$this->assertSame(Avatar::class, $avatar->getModel());
3839
$this->assertSame('id', $avatar->getLocalKey());
3940
$this->assertSame('user_id', $avatar->getForeignKey());
41+
42+
$avatar = $relations['comments'];
43+
44+
$this->assertInstanceOf(ModelRelation::class, $avatar);
45+
$this->assertSame('comments', $avatar->getName());
46+
$this->assertSame('BelongsToMany', $avatar->getType());
47+
$this->assertSame(Comment::class, $avatar->getModel());
48+
$this->assertSame(null, $avatar->getLocalKey());
49+
$this->assertSame(null, $avatar->getForeignKey());
4050
}
4151
}

tests/Models/User.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ public function avatar()
2222
return $this->hasOne(Avatar::class);
2323
}
2424

25+
public function comments()
26+
{
27+
return $this->belongsToMany(Comment::class);
28+
}
29+
2530
}

0 commit comments

Comments
 (0)