ajt
ajt

Reputation: 662

cant save new record as model is not found

I am getting a 'Call to a member function create() on a non-object' error on a simple save where the same code works in another project. The names are correct for the model and the table of that name exists in mysql called trequestamends. I am sure it is a simple error but I cant find the issue and I checked the docs

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');  

Upvotes: 0

Views: 48

Answers (1)

Jinank Thakker
Jinank Thakker

Reputation: 121

I think the problem is in name conventions in cakephp, you should change your table name from "trequestamends" to "trequest_amends" and your model name will be "TrequestAmend" and then you can call create method by $this->TrequestAmend->create(); Hope it will work!

Upvotes: 1

Related Questions