From fb46e265544cc57c5afc8c9b1ff903c19e48dcee Mon Sep 17 00:00:00 2001 From: John Koster Date: Mon, 29 Aug 2022 08:58:39 -0500 Subject: [PATCH] Adds default entity type values for backwards compatibility #7 --- src/EntryRelationship.php | 4 ++-- tests/OneToManyTest.php | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/EntryRelationship.php b/src/EntryRelationship.php index 49b8d68..9fd7400 100644 --- a/src/EntryRelationship.php +++ b/src/EntryRelationship.php @@ -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; @@ -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; diff --git a/tests/OneToManyTest.php b/tests/OneToManyTest.php index a5645ef..fc5ddff 100644 --- a/tests/OneToManyTest.php +++ b/tests/OneToManyTest.php @@ -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()