Skip to content

Commit 4742e4c

Browse files
committed
Fix up assoc bug in findBy magic.
1 parent a5d9227 commit 4742e4c

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

src/Method/AssociationTableMixinClassReflectionExtension.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public function hasMethod(ClassReflection $classReflection, string $methodName):
5757
return false;
5858
}
5959

60+
// magic findBy* method on Association
61+
if (preg_match('/^find(?:\w+)?By/', $methodName) > 0) {
62+
return true;
63+
}
64+
6065
return $this->getTableReflection()->hasMethod($methodName);
6166
}
6267

@@ -72,6 +77,11 @@ public function getMethod(ClassReflection $classReflection, string $methodName):
7277
return new TableFindByPropertyMethodReflection($methodName, $classReflection);
7378
}
7479

80+
// magic findBy* method on Association
81+
if ($classReflection->isSubclassOf(Association::class) && preg_match('/^find(?:\w+)?By/', $methodName) > 0) {
82+
return new TableFindByPropertyMethodReflection($methodName, $this->getTableReflection());
83+
}
84+
7585
return $this->getTableReflection()->getNativeMethod($methodName);
7686
}
7787

tests/test_app/Model/Table/NotesTable.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ public function warning(): array
5252
$this->MyUsers->logLastLogin($user);
5353
$article = $this->MyUsers->Articles->newSample();
5454
$article->id = '002';
55+
// Test magic findBy methods on association chains (fixes issue #51)
56+
$articleQuery = $this->MyUsers->Articles->findByTitle('Test Title');
57+
$foundArticle = $articleQuery->first();
58+
// Test findBy with And operator
59+
$articleAndQuery = $this->MyUsers->Articles->findByTitleAndActive('Test', true);
60+
// Test findBy with Or operator
61+
$articleOrQuery = $this->MyUsers->Articles->findByTitleOrActive('Test', true);
5562
$entity = $this->get(10, cache: 'my_cache');
5663
if ($entity->note === 'Test') {
5764
$entity = $this->newEmptyEntity();

tests/test_app/Model/Table/UsersTable.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,8 @@ public function logLastLogin(User $user): User
4545
*/
4646
public function blockOld(): void
4747
{
48+
// Test magic findBy methods on association (issue #51)
49+
$articleQuery = $this->Articles->findByTitle('Test');
50+
$article = $articleQuery->first();
4851
}
4952
}

0 commit comments

Comments
 (0)