我是CakePHP的新手。我正在使用cakephp 3.2
我创建了一个表stock_exchanges和控制器StockExchangesController.php
我的控制器代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.vikash.mydrawingapp.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="clear"
android:onClick="clearCanvas"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
我想在未创建表类的情况下访问表。
以下行给出了错误
<?php
namespace App\Controller;
use Cake\ORM\TableRegistry;
use Cake\Core\Configure;
use Cake\Network\Exception\NotFoundException;
use Cake\View\Exception\MissingTemplateException;
class StockExchangesController extends AppController
{
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash'); // Include the FlashComponent
}
public function index()
{
$stockExchanges = $this->StockExchanges->find('all');
$this->set(compact('stockExchanges'));
}
}
?>
错误是
错误:在非对象上调用成员函数find() 文件D:\ wamp \ www \ Projects \ cakephp \ src \ Controller \ StockExchangesController.php 行:20
修改 @AIPD TECH在下面的评论中提到
$ stockExchanges = TableRegistry :: get(&#39; StockExchanges&#39;);
如果表名是帖子,那么我可以使用$ this-&gt; Posts-&gt; find(&#39; all&#39;); 。那么$ this-&gt; StockExchanges-&gt; find(&#39; all&#39;);不是用下划线访问表格的方法吗?我必须使用TableRegistry吗?
另外,请注意我无法访问我的控制器StockExchangesController作为/ stock_exchanges /,我只能访问/ stockexchanges /
请帮忙。
巴尔加瓦
堆栈追踪:
( ! ) Xdebug: user triggered in D:\wamp\www\Projects\cakephp\vendor\cakephp\cakephp\src\Template\Error\fatal_error.ctp on line 36 Call Stack #TimeMemoryFunctionLocation 10.0010157320{main}( )..\index.php:0 20.14903911888Cake\Routing\Dispatcher->dispatch( )..\index.php:37 30.20804779240Cake\Routing\Dispatcher->_invoke( )..\Dispatcher.php:87 40.27805081520Cake\Controller\Controller->invokeAction( )..\Dispatcher.php:114 50.27805081848call_user_func_array:{D:\wamp\www\Projects\cakephp\vendor\cakephp\cakephp\src\Controller\Controller.php:429} ( )..\Controller.php:429 60.27805081984App\Controller\StockExchangesController->index( )..\Controller.php:429 70.28405078216Cake\Error\BaseErrorHandler->Cake\Error\{closure}( )..\BaseErrorHandler.php:0 80.28505079400Cake\Error\BaseErrorHandler->handleFatalError( )..\BaseErrorHandler.php:98 90.32005314816Cake\Error\BaseErrorHandler->handleException( )..\BaseErrorHandler.php:217 100.32005314920Cake\Error\ErrorHandler->_displayException( )..\BaseErrorHandler.php:179 110.33305417224Cake\Error\ExceptionRenderer->render( )..\ErrorHandler.php:144 120.33405421048Cake\Error\ExceptionRenderer->_outputMessage( )..\ExceptionRenderer.php:192 130.33405421296Cake\Controller\Controller->render( )..\ExceptionRenderer.php:320 140.35505699392Cake\View\View->render( )..\Controller.php:611 150.35705701304Cake\View\View->_render( )..\View.php:587 160.35805702264Cake\View\View->_evaluate( )..\View.php:952 170.35905727864include( 'D:\wamp\www\Projects\cakephp\vendor\cakephp\cakephp\src\Template\Error\fatal_error.ctp' )..\View.php:992 180.36005745400xdebug_print_function_stack ( )..\fatal_error.ctp:36