CakePhp错误:在非对象上调用成员函数create()

Inv*_*tax 1 php oop cakephp

我目前正在学习如何使用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');
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

但是在我看来,我收到了这个错误:

错误:在非对象上调用成员函数create()

为什么会出现此错误,如何解决?

我很抱歉,我相信它引用的行不是在Controller中,而是在我看来.它指的是我的观点,它有以下几行:

<?php echo $form->create('Format');?>
Run Code Online (Sandbox Code Playgroud)

在使用它之前我还需要声明一些其他内容吗?即$this->Format->create();

dec*_*nda 5

你应该使用:

  $this->Form->create('Format');
Run Code Online (Sandbox Code Playgroud)

删除

 <?php echo $form->create('Format');?> 
Run Code Online (Sandbox Code Playgroud)

并替换它

 <?php echo $this->Form->create('Format');?> 
Run Code Online (Sandbox Code Playgroud)

$ form是导致错误的那个.