Why am I getting “TableRegistry not found” in CakePhP 3.0?

核能气质少年 提交于 2020-01-03 17:01:24

问题


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


回答1:


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




回答2:


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;


来源:https://stackoverflow.com/questions/34157865/why-am-i-getting-tableregistry-not-found-in-cakephp-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!