Joe C
Joe C

Reputation: 2834

Why am I getting "TableRegistry not found" in CakePhP 3.0?

I have a class full of static functions that I call UtilityFunctions in my Model/ directory. But that class cannot access TableRegistry::Get even though the "use" statements are in place. Below is the code and the error.

namespace App\Model\Table;
use Cake\ORM\TableRegistry;
use App\Model\Entity\Device;

class UtilityFunctions {
    public static function getDevice($deviceInfo) {
        $devicesTable = TableRegistry::get('Devices'); // TableRegistry not found
        $query = $devicesTable->findByDeviceInfo($deviceInfo);
       ...
    }
}

"Class \u0027UtilityFunctions\TableRegistry\u0027 not found", "/var/www/myserver/src/Model/Custom/UtilityFunctions.php",115

Upvotes: 4

Views: 6568

Answers (2)

JvO
JvO

Reputation: 3106

It depends on the order of the lines. If you place namespace first, PHP will look inside the given namespace unless you specify a \ first in the use statement . E.g.

namespace App\Model\Table;
use Cake\ORM\TableRegistry;

will look for App\Model\Table\Cake\ORM\TableRegistry, which obviously isn't right.

But this will work:

use Cake\ORM\TableRegistry;
namespace App\Model\Table;

Or

namespace App\Model\Table;
use \Cake\ORM\TableRegistry;

Upvotes: -1

Nathaniel Cutajar
Nathaniel Cutajar

Reputation: 312

I know that this might be a really late reply, but it may be an issue with you not inserting use Cake\ORM\TableRegistry; at the top of the code.

I had the same issue. I added that line of code and it worked

Upvotes: 9

Related Questions