[英]Cakephp 3 Default Model not loaded in controller
I have a controller name CodeBridgeController
and have a model table name CodeBridgeTable
. 我有一个控制器名称
CodeBridgeController
和一个模型表名称CodeBridgeTable
。 When I call CodeBridge table in action like following: 当我在调用CodeBridge表时,如下所示:
$this->CodeBridge->find('all')->toArray();
This generate Error: Call to a member function find() on a non-object File D:\\php\\htdocs\\cake3\\erp\\src\\Controller\\CodeBridgeController.php Line: 25
I think this is silly matter but i could not find any wrong in the code why table is not loaded in this context. 这会产生
Error: Call to a member function find() on a non-object File D:\\php\\htdocs\\cake3\\erp\\src\\Controller\\CodeBridgeController.php Line: 25
我认为这很愚蠢,但我找不到代码中的任何错误,为什么不在此上下文中加载表。 When i call this way it works. 当我这样称呼它的时候。
$codeBridgeTable = TableRegistry::get('CodeBridge'); $results = $codeBridgeTable->find('all')->hydrate(false)->toArray();
What may create the problem, expert suggestion needed. 什么可能造成问题,需要专家建议。
Finally, I found where the problem lies. 最后,我找到了问题所在。 In controller action, I generating a url like following:
Router::url(array('controller' =>'codebridge','action'=>'edit'));
在控制器动作中,我生成一个如下所示的URL:
Router::url(array('controller' =>'codebridge','action'=>'edit'));
which generate url /cake3/erp/codebridge/edit
生成URL
/cake3/erp/codebridge/edit
After changing the controller name in camel case Router::url(array('controller' =>'CodeBridge','action'=>'edit'));
在骆驼的情况下更改控制器名称后
Router::url(array('controller' =>'CodeBridge','action'=>'edit'));
which generate url '/cake3/erp/code-bridge/edit` 生成url'/ cake3 / erp / code-bridge / edit`
This solve the problem 这样解决问题
To resolve this, just load the model prior to trying to use it: 要解决此问题,只需在尝试使用模型之前加载模型:
$this->loadModel("CodeBridge");
$this->CodeBridge->find('all')->toArray();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.