由于找不到模型,因此无法保存新记录

时间:2014-09-16 07:19:15

标签: cakephp

我在一个简单的保存中得到一个'对一个非对象的成员函数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');  

1 个答案:

答案 0 :(得分:1)

我认为问题出在cakephp的名称约定中,您应该将表格名称从“trequestamends”更改为“trequest_amends”,并且您的模型名称将为“TrequestAmend”,然后您可以通过$ this-&gt;调用create method。 TrequestAmend-&GT;创建(); 希望它能奏效!