cakePHP - 在非对象上调用成员函数findById()

时间:2013-11-06 11:50:27

标签: cakephp

我是CakePHP的新手,请指教。

数据库名称为admin_accounts。这是数据。 enter image description here  

类文件

<?php
App::uses('AppModel', 'Model');

class AdminAccount extends AppModel {

}
?>

控制器文件

<?php
    class AdminAccountsController extends AppController{

    public function profile($id = null){
        if(!$id){ throw new NotFoundException(__('No profile')); }

        $adminAccount = $ $this->AdminAccount->findById($id);
        if (!$adminAccount) {
                throw new NotFoundException(__('No profile'));
        }
        $this->set('admin_account', $adminAccount);
    }

    }
?>

当我调用个人资料时,它给了我这个错误

Warning (4096): Object of class AdminAccountsController could not be converted to string   [APP/Controller/AdminAccountsController.php, line 15]
Notice (8): Object of class AdminAccountsController to string conversion [APP/Controller/AdminAccountsController.php, line 15]
Notice (8): Undefined variable: Object [APP/Controller/AdminAccountsController.php, line 15]
Notice (8): Trying to get property of non-object [APP/Controller/AdminAccountsController.php, line 15]
Fatal error: Call to a member function findById() on a non-object in /home/crayzest/public_html/cake/app/Controller/AdminAccountsController.php on line 15

我做错了什么?

1 个答案:

答案 0 :(得分:0)

首先删除额外的$符号。

然后,在使用findById()函数之前加载Model。

在findbyId()语句之前写这个

$this->loadModel('AdminAccount');