php CakePHP:在非对象上调用成员函数 find()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6716456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CakePHP: Call to a member function find() on a non-object
提问by Cameron
I get the following errors when viewing my Admin Index
查看管理索引时出现以下错误
Notice (8): Undefined property: ClientsController::$Clients [APP/controllers/clients_controller.php, line 27]
Call to a member function find() on a non-object in /Users/cameron/Sites/crm/app/controllers/clients_controller.php on line 27
here is the code:
这是代码:
class ClientsController extends AppController
{
var $name = 'Clients';
function beforeFilter()
{
parent::beforeFilter();
$this->Auth->allow(array('*'));
}
function index()
{
$this->set('clients', $this->Clients->find('all'));
}
function view ( $id, $slug )
{
$article = $this->Clients->read(null, Tiny::reverseTiny($id));
$this->set(compact('client'));
}
function admin_index()
{
$this->set('clients', $this->Clients->find('all'));
}
Any ideas what the issue is here? (I have created a Model also)
任何想法这里的问题是什么?(我也创建了一个模型)
回答by u481547
Some potential issues:
一些潜在的问题:
Normally
一般
var $name = 'Client'; // Not Clients
You have a Client, and the controller is for "Clients".
您有一个客户端,控制器用于“客户端”。
Try:
尝试:
$this->set('clients', $this->Client->find('all'));
with the above suggestion on var $name
.
有了上面的建议var $name
。
See a more complete list of potential solutions here if that doesn't lead you down the right path.
如果这不能引导您走上正确的道路,请在此处查看更完整的潜在解决方案列表。
回答by dhofstet
Model names are singular in CakePHP (at least if you follow the naming conventions), which means you have to use: $this->Client->find('all');
CakePHP 中的模型名称是单数的(至少如果您遵循命名约定),这意味着您必须使用: $this->Client->find('all');