【问题标题】:cant save new record as model is not found由于找不到模型,无法保存新记录
【发布时间】:2014-09-16 07:19:14
【问题描述】:

我在简单保存时收到“调用非对象上的成员函数 create()”错误,其中相同的代码在另一个项目中工作。名称对于模型是正确的,并且该名称的表存在于名为 trequestamends 的 mysql 中。 我确定这是一个简单的错误,但我找不到问题,我检查了文档

http://book.cakephp.org/2.0/en/models/saving-your-data.html http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html

 public function trequestAmend($id=0) {
...
      if ($this->request->is('post')) {
            $this->Trequestamend->create();
            if ($this->Trequestamend->save($this->request->data)) {
                $this->Session->setFlash(__('Your Tutor Requested Amended data has been saved.'));
               // return $this->redirect(array('action' => 'displayall'));
            }
            $this->Session->setFlash(__('Unable to add your post.'));
       }

view

 echo $this->Form->create('Trequestamend');
       echo $this->Form->input($lessonId, array('type' => 'hidden'));
        echo $this->Form->input($tutorId, array('type' => 'hidden'));
       echo $this->Form->input($stId, array('type' => 'hidden'));

         echo  "<b>Change times to </b>" . $this->Form->input('start_time', array(   'type' => 'time',  'selected' => '13:00:00'));
       echo    $this->Form->input('end_time', array(   'type' => 'time',  'selected' => array('hour' => '1','minute' => '30','meridian' => 'pm')));
         echo    $this->Form->input('lesson_date', array(   'type' => 'date', 'dateFormat' => 'DMY', 'minYear' => date('Y'), 'maxYear' => date('Y')+1));
          echo   $this->Form->input('reason', array('label' => 'Reasons for Lesson Amendment', 'style' => 'width: 300px; height: 50px;'));
       echo $this->Form->end('Save Post');  

【问题讨论】:

  • 你的控制器名称是什么
  • 函数同名,加t加复数
  • 您的控制器名称是“TrequestAmendsController”??

标签: cakephp


【解决方案1】:

我认为问题在于 cakephp 中的命名约定,您应该将表名从“trequestamends”更改为“trequest_amends”,您的模型名称将是“TrequestAmend”,然后您可以通过 $this->TrequestAmend 调用 create 方法->创建(); 希望它会工作!

【讨论】:

  • 好吧,有点工作。我按照你说的做了名字,我没有收到错误。由于某种原因,隐藏字段不会保存。我将再次检查文档
猜你喜欢
  • 2015-03-14
  • 2011-12-17
  • 1970-01-01
  • 2013-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多