简体   繁体   中英

Cakephp 3 Default Model not loaded in controller

I have a controller name CodeBridgeController and have a model table name CodeBridgeTable . When I call CodeBridge table in action like following:

$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. 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')); which generate url /cake3/erp/codebridge/edit

After changing the controller name in camel case Router::url(array('controller' =>'CodeBridge','action'=>'edit')); which generate 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM