Skip to content

Commit

Permalink
Dont lookup belongstomany keys
Browse files Browse the repository at this point in the history
mpociot committed Jul 4, 2018
1 parent 53c2c8c commit 517ace1
Showing 3 changed files with 17 additions and 7 deletions.
5 changes: 0 additions & 5 deletions src/RelationFinder.php
Original file line number Diff line number Diff line change
@@ -72,11 +72,6 @@ protected function getRelationshipFromMethodAndModel(ReflectionMethod $method, s
$localKey = $return->getForeignKey();
}

if ($return instanceof BelongsToMany && ! $return instanceof MorphToMany) {
$foreignKey = $this->getParentKey($return->getQualifiedOwnerKeyName());
$localKey = $return->getForeignKey();
}

return [
$method->getName() => new ModelRelation(
$method->getShortName(),
14 changes: 12 additions & 2 deletions tests/GetModelRelationsTest.php
Original file line number Diff line number Diff line change
@@ -2,13 +2,12 @@

namespace BeyondCode\ErdGenerator\Tests;

use BeyondCode\ErdGenerator\GraphBuilder;
use BeyondCode\ErdGenerator\ModelRelation;
use BeyondCode\ErdGenerator\RelationFinder;
use BeyondCode\ErdGenerator\Tests\Models\Post;
use BeyondCode\ErdGenerator\Tests\Models\User;
use BeyondCode\ErdGenerator\Tests\Models\Avatar;
use BeyondCode\ErdGenerator\GenerateDiagramCommand;
use BeyondCode\ErdGenerator\Tests\Models\Comment;

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

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

$this->assertCount(3, $relations);

$posts = $relations['posts'];

$this->assertInstanceOf(ModelRelation::class, $posts);
@@ -37,5 +38,14 @@ public function it_can_find_model_relations()
$this->assertSame(Avatar::class, $avatar->getModel());
$this->assertSame('id', $avatar->getLocalKey());
$this->assertSame('user_id', $avatar->getForeignKey());

$avatar = $relations['comments'];

$this->assertInstanceOf(ModelRelation::class, $avatar);
$this->assertSame('comments', $avatar->getName());
$this->assertSame('BelongsToMany', $avatar->getType());
$this->assertSame(Comment::class, $avatar->getModel());
$this->assertSame(null, $avatar->getLocalKey());
$this->assertSame(null, $avatar->getForeignKey());
}
}
5 changes: 5 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
@@ -22,4 +22,9 @@ public function avatar()
return $this->hasOne(Avatar::class);
}

public function comments()
{
return $this->belongsToMany(Comment::class);
}

}

0 comments on commit 517ace1

Please sign in to comment.