@@ -48,12 +48,12 @@ protected function getTableReflection(): ClassReflection
4848     */ 
4949    public  function  hasMethod (ClassReflection $ classReflectionstring  $ methodNamebool 
5050    {
51-         // Check if it's a native method first, before treating as magic  
51+         // Handle Table classes (including specific table classes accessed through associations)  
5252        if  ($ classReflectionis (Table::class)) {
5353            if  ($ classReflectionhasNativeMethod ($ methodName
5454                return  false ; // Let the native method be used 
5555            }
56-             // magic findBy* method 
56+             // magic findBy* method - available on ALL table classes  
5757            if  (preg_match ('/^find\w*By/ ' , $ methodName0 ) {
5858                return  true ;
5959            }
@@ -63,12 +63,11 @@ public function hasMethod(ClassReflection $classReflection, string $methodName):
6363            return  false ;
6464        }
6565
66-         // magic findBy* method on Association 
67-         // NOTE: We cannot check for native methods on the associated table class here 
68-         // because MethodsClassReflectionExtension doesn't have access to generic type parameters. 
69-         // This means custom methods matching find*By pattern will be treated as magic methods 
70-         // when accessed through associations. Use @method annotations to override. 
66+         // For associations with find*By methods, check if there's a concrete table class 
67+         // that can be determined from the property annotation 
7168        if  (preg_match ('/^find\w*By/ ' , $ methodName0 ) {
69+             // TODO: Try to extract the table class from generic type parameter here 
70+             // For now, we return true to indicate we have the magic method 
7271            return  true ;
7372        }
7473
@@ -82,23 +81,28 @@ public function hasMethod(ClassReflection $classReflection, string $methodName):
8281     */ 
8382    public  function  getMethod (ClassReflection $ classReflectionstring  $ methodNameMethodReflection 
8483    {
85-         // Check if it's a native method first, before treating as magic  
84+         // Handle Table classes (including specific table classes accessed through associations)  
8685        if  ($ classReflectionis (Table::class)) {
8786            if  ($ classReflectionhasNativeMethod ($ methodName
8887                return  $ classReflectiongetNativeMethod ($ methodName
8988            }
90-             // magic findBy* method 
89+             // magic findBy* method - use the SPECIFIC table class for reflection 
90+             // This ensures that when accessed through Association<SpecificTable>, 
91+             // the magic method is created with the correct table class context 
9192            if  (preg_match ('/^find\w*By/ ' , $ methodName0 ) {
9293                return  new  TableFindByPropertyMethodReflection ($ methodName$ classReflection
9394            }
9495        }
9596
96-         // magic findBy* method on Association  
97+         // For associations, handle magic find*By methods  
9798        $ associationReflection$ this reflectionProvider ->getClass (Association::class);
9899        if  (
99100            $ classReflectionisSubclassOfClass ($ associationReflection
100101            && preg_match ('/^find\w*By/ ' , $ methodName0 
101102        ) {
103+             // Return magic method based on base Table class 
104+             // LIMITATION: This doesn't know about the specific table class from generic parameter 
105+             // So custom findOrCreateBy methods on specific tables won't be detected through associations 
102106            return  new  TableFindByPropertyMethodReflection ($ methodName$ this getTableReflection ());
103107        }
104108
0 commit comments