How to have a different return type other than Query for finder functions in Behavior

We have successfully upgraded from CakePHP 3.10 to CakePHP 4.0 and currently in the process of migrating to 4.0 as per the 4.0 Migration Guide.

One of the major change that we noticed in 4.0 is that the finder functions in the Behavior now only allows query as the return type. We have many shared finder functions inside the Behavior (like the one below) which process the query and return the array, string or bool (Before upgrade they work just fine).

public function findUserModules(Query $query,array $options){
    $activeModules = [];
    $userSubscriptions = $query->where(['user_id' => $options['id']])->first();
    if (!empty($userSubscriptions)) {
        ...
        $activeModules = $userSubscriptions['active'];
    }
    return $activeModules;
}

These functions are now throwing the error:

2022-04-07 09:54:16 Error: [TypeError] Return value of Cake\ORM\BehaviorRegistry::callFinder() must be an instance of Cake\ORM\Query, array returned in .../vendor/cakephp/cakephp/src/ORM/BehaviorRegistry.php on line 275

I understand that we would have to update the finder functions to normal mixin methods. However, it requires major update throughout the application. I would like to know if there’s any work around to allow a different return type for the finder functions.

Finders were always supposed to return queries, you just got lucky (or unlucky, seeing that it bites you in the butt now) that your usage slipped through because 3.x had no native return type hints.

I’m pretty sure there’s no “workaround” other than renaming your methods, as the return type for finders is set, and you cannot change it.

1 Like

This is presumably only a problem if you call these via ->find('userModules')? You should be able to call ->findUserModules(...) if you really want to keep the names the same? But yeah, I’d think that getUserModules might be a better name.