CakePHP:在非对象上调用成员函数find()

时间:2011-07-16 09:16:05

标签: php cakephp

查看管理员索引时出现以下错误

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

这是代码:

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

任何想法问题在这里? (我也创建了一个模型)

2 个答案:

答案 0 :(得分:3)

一些潜在的问题:

通常

var $name = 'Client'; // Not Clients

您有一个客户端,控制器用于“客户端”。

尝试:

$this->set('clients', $this->Client->find('all'));

var $name上提出上述建议。

如果不能引导您走上正确的道路,请在此处查看更完整的潜在解决方案列表。

CakePHP call to member function on non-object

答案 1 :(得分:1)

模型名在CakePHP中是单数的(至少如果你遵循命名约定),这意味着你必须使用:$this->Client->find('all');