diff --git a/src/Method/AssociationTableMixinClassReflectionExtension.php b/src/Method/AssociationTableMixinClassReflectionExtension.php index 52e578f..0e4fa58 100644 --- a/src/Method/AssociationTableMixinClassReflectionExtension.php +++ b/src/Method/AssociationTableMixinClassReflectionExtension.php @@ -57,6 +57,11 @@ public function hasMethod(ClassReflection $classReflection, string $methodName): return false; } + // magic findBy* method on Association + if (preg_match('/^find(?:\w+)?By/', $methodName) > 0) { + return true; + } + return $this->getTableReflection()->hasMethod($methodName); } @@ -72,6 +77,11 @@ public function getMethod(ClassReflection $classReflection, string $methodName): return new TableFindByPropertyMethodReflection($methodName, $classReflection); } + // magic findBy* method on Association + if ($classReflection->isSubclassOf(Association::class) && preg_match('/^find(?:\w+)?By/', $methodName) > 0) { + return new TableFindByPropertyMethodReflection($methodName, $this->getTableReflection()); + } + return $this->getTableReflection()->getNativeMethod($methodName); } diff --git a/tests/test_app/Model/Table/NotesTable.php b/tests/test_app/Model/Table/NotesTable.php index 9ce9669..317b42c 100644 --- a/tests/test_app/Model/Table/NotesTable.php +++ b/tests/test_app/Model/Table/NotesTable.php @@ -52,6 +52,13 @@ public function warning(): array $this->MyUsers->logLastLogin($user); $article = $this->MyUsers->Articles->newSample(); $article->id = '002'; + // Test magic findBy methods on association chains (fixes issue #51) + $articleQuery = $this->MyUsers->Articles->findByTitle('Test Title'); + $foundArticle = $articleQuery->first(); + // Test findBy with And operator + $articleAndQuery = $this->MyUsers->Articles->findByTitleAndActive('Test', true); + // Test findBy with Or operator + $articleOrQuery = $this->MyUsers->Articles->findByTitleOrActive('Test', true); $entity = $this->get(10, cache: 'my_cache'); if ($entity->note === 'Test') { $entity = $this->newEmptyEntity(); diff --git a/tests/test_app/Model/Table/UsersTable.php b/tests/test_app/Model/Table/UsersTable.php index 18b2d8c..f16b876 100644 --- a/tests/test_app/Model/Table/UsersTable.php +++ b/tests/test_app/Model/Table/UsersTable.php @@ -45,5 +45,8 @@ public function logLastLogin(User $user): User */ public function blockOld(): void { + // Test magic findBy methods on association (issue #51) + $articleQuery = $this->Articles->findByTitle('Test'); + $article = $articleQuery->first(); } }