【发布时间】:2014-08-23 23:04:14
【问题描述】:
我目前正在学习如何使用 CakePhp。
我在自定义控制器中创建了如下函数:
class FormatsController extends AppController
{
// ....
function admin_add()
{
// if the form data is not empty
if (!empty($this->data)) {
// initialise the format model
$this->Format->create();
// create the slug
$this->data['Format']['slug'] = $this->slug($this->data['Format']['name']);
// try saving the format
if ($this->Format->save($this->data)) {
// set a flash message
$this->Session->setFlash('The Format has been saved', 'flash_good');
// redirect
$this->redirect(array('action' => 'index'));
} else {
// set a flash message
$this->Session->setFlash('The Format could not be saved. Please, try again.', 'flash_bad');
}
}
}
}
但是在我看来,我收到了这个错误:
错误:在非对象上调用成员函数 create()
为什么会出现这个错误,我该如何解决?
抱歉,我相信它所引用的行不是在控制器中,而是在我看来。它指的是我的观点,其中有以下几行:
<?php echo $form->create('Format');?>
在使用它之前我还需要声明什么吗?即$this->Format->create();
【问题讨论】:
-
请参阅下面我的答案..您应该使用 $this->Form->create('Format');删除您的 create('Format');?> 并将其替换为 Form->create('Format');?> $form 是导致错误。
-
在你当前的控制器中,你应该检查属性
$uses这样public $uses = array('User', .. );我们必须在使用前调用模型User。