【问题标题】:CakePhp Error: Call to a member function create() on a non-objectCakePhp 错误:在非对象上调用成员函数 create()
【发布时间】: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-&gt;Format-&gt;create();

【问题讨论】:

  • 请参阅下面我的答案..您应该使用 $this->Form->create('Format');删除您的 create('Format');?> 并将其替换为 Form->create('Format');?> $form 是导致错误。
  • 在你当前的控制器中,你应该检查属性$uses这样public $uses = array('User', .. );我们必须在使用前调用模型User

标签: php oop cakephp


【解决方案1】:

试试

$this->Form->create(null,['url' => ['controller' => 'yourController', 'action' => 'yourAction']])

【讨论】:

    【解决方案2】:

    您应该使用:

      $this->Form->create('Format');
    

    删除

     <?php echo $form->create('Format');?> 
    

    并将其替换为

     <?php echo $this->Form->create('Format');?> 
    

    $form 是导致错误的原因。

    【讨论】:

    • @Imran 检查这个答案
    • 我用了你所说的,它奏效了,但我正在阅读的书告诉我另一种方式是正确的。实际上那本书是 2008 年出版的,所以自 2008 年以来可能发生了很多变化。谢谢。
    • @IWannaKnow 是的,cake 有很多更新 :) 你必须查看他们的官方文档book.cakephp.org/2.0/en/index.html
    • 我在阅读“CakePHP 应用程序开发”一书后遇到了同样的问题。但也有文件和文件夹命名的问题。似乎 cakePHP 经常变化。
    • @theme 确实如此,这就是为什么有时我习惯于反复试验的方法,有时还会阅读除手册和其他蛋糕书之外的其他资源。
    【解决方案3】:

    需要定义模型的全局名称。因此,可以在应用程序的任何位置访问它。

    例如:我的模型是用户

    class User extends AppModel {
        var $name = 'User';
    
        function myfunction ($id) {
                      .....
            }
    }
    To use in controller
    Controller: 
    
    class UsersController extends AppController
    {
        function test()
        {
          $this->User->myfunction();
            ......
        }
    }
    

    希望对你有帮助!

    【讨论】:

    • 会不会是我定义的复数var $name = 'Formats';?尽管如此,所有其他功能都可以正常工作,例如$formats = $this-&gt;Format-&gt;find('all', array('conditions' =&gt; array('status' =&gt; 1)));
    【解决方案4】:
    $this->Format
    

    是未定义的(所以它的值为null),一个null对象没有任何功能,所以你不能使用

    $this->Format->create();
    

    几乎等于

    null->create();
    

    【讨论】:

      【解决方案5】:

      在你的行动中试试这个

      $this->loadModel('Format');
      

      【讨论】:

        【解决方案6】:

        您是否创建了“格式”模型? 当被调用的模型有问题时,就会出现这种错误。要么未创建,要么未正确创建,要么未正确导入/启动。

        如果您在控制器中声明了 $uses 变量,请确保在您的 $uses 数组中与其他模型一起包含“格式”。

        【讨论】:

          【解决方案7】:

          这可能是由于某种原因 $this->Format 没有被创建。如果你查看你的代码 sn-p 你会看到它调用了 create() 函数。在调用 create() 之前将其作为调试语句添加到控制器函数中以查看它是否已设置。

          debug( isset( $this->Format ) );
          

          如果设置应该输出true。如果您尝试这样做,请告诉我它的内容,我可能还有其他一些建议可供参考。

          【讨论】:

          • 格式控制器确实存在,正在调用其他函数并成功加载(即视图和索引)。我认为使用复数名称可能是问题(它的名称 FormatsController),但如果可以加载其他函数,这不是问题吗?
          • 您需要验证的不是 FormatsController.php,而是根据您的代码存储在 $this->Format 中的模型 Format.php。那是似乎未定义的对象,您需要进行测试以验证。
          • 已经定义了一个名为 Format.php 的方法,它包含这一行:var $name = 'Format';
          猜你喜欢
          • 2012-02-06
          • 1970-01-01
          • 2012-02-18
          • 1970-01-01
          • 1970-01-01
          • 2017-07-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多