Skip to content

Commit

Permalink
Adds default entity type values for backwards compatibility #7
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnathonKoster committed Aug 29, 2022
1 parent 90195fb commit fb46e26
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/EntryRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function collection($collection)
$this->leftCollection = $collection;
}

public function field($handle, $entityType)
public function field($handle, $entityType = 'entry')
{
$this->leftField = $handle;
$this->leftType = $entityType;
Expand All @@ -53,7 +53,7 @@ public function isRelatedTo($rightCollectionHandle)
return $this;
}

public function through($rightCollectionFieldHandle, $entityType)
public function through($rightCollectionFieldHandle, $entityType = 'entry')
{
$this->rightField = $rightCollectionFieldHandle;
$this->rightType = $entityType;
Expand Down
35 changes: 35 additions & 0 deletions tests/OneToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,41 @@ public function test_one_to_many_relationships()
$this->assertSame([], Entry::find('authors-2')->get('books', []));
}

public function test_explicit_one_to_many()
{
Relate::clear();
Relate::collection('books')
->field('author')
->isRelatedTo('authors')
->through('books')
->manyToOne();

Relate::collection('authors')
->field('books')
->isRelatedTo('books')
->through('author')
->oneToMany();

Entry::find('books-1')->set('author', 'authors-1')->save();

$this->assertSame(['books-1'], Entry::find('authors-1')->get('books', []));

Entry::find('books-2')->set('author', 'authors-1')->save();

$this->assertSame(['books-1', 'books-2'], Entry::find('authors-1')->get('books', []));

Entry::find('books-2')->set('author', 'authors-2')->save();

$this->assertSame(['books-1'], Entry::find('authors-1')->get('books', []));
$this->assertSame(['books-2'], Entry::find('authors-2')->get('books', []));

Entry::find('books-1')->set('author', null)->save();
Entry::find('books-2')->set('author', null)->save();

$this->assertSame([], Entry::find('authors-1')->get('books', []));
$this->assertSame([], Entry::find('authors-2')->get('books', []));
}

public function test_one_to_many_user_relationships()
{
Relate::clear()
Expand Down

0 comments on commit fb46e26

Please sign in to comment.