【问题标题】:Why am I getting "TableRegistry not found" in CakePhP 3.0?为什么我在 CakePhP 3.0 中得到“找不到 TableRegistry”?
【发布时间】:2016-03-13 11:45:19
【问题描述】:

我的 Model/ 目录中有一个充满静态函数的类,我称之为 UtilityFunctions。但是即使“使用”语句到位,该类也无法访问 TableRegistry::Get。下面是代码和错误。

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);
       ...
    }
}

"找不到类\u0027UtilityFunctions\TableRegistry\u0027", "/var/www/myserver/src/Model/Custom/UtilityFunctions.php",115

【问题讨论】:

  • 我认为是因为它是在静态函数中调用的,尝试在开头添加反斜杠 - \TableRegistry::get('Devices')
  • 你是怎么调用那个方法的?

标签: mysql cakephp-3.0


【解决方案1】:

我知道这可能是一个很晚的回复,但您没有在代码顶部插入 use Cake\ORM\TableRegistry; 可能是个问题。

我有同样的问题。我添加了那行代码,它工作了

【讨论】:

    【解决方案2】:

    这取决于行的顺序。如果您首先放置 namespace,PHP 将在给定的命名空间内查找,除非您在 use 语句中首先指定 \。例如

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

    会寻找 App\Model\Table\Cake\ORM\TableRegistry,这显然是不对的。

    但这会起作用:

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

    或者

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

    【讨论】:

      猜你喜欢
      • 2023-04-10
      • 1970-01-01
      • 2019-07-09
      • 1970-01-01
      • 2012-05-17
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 2012-09-12
      相关资源
      最近更新 更多