简体   繁体   中英

CakePHP Fatal error: Call to a member function create() on a non-object (Controller)

I have CakePHP Controller code which is throwing up the following error 'Fatal error: Call to a member function create() on a non-object'.

The controller code is as follows:

if ($this->request->is('post')) {
            $this->MonthlyReturn->create();
            $this->MonthlyReturn->saveField('company_id', $cid);    // Assign current company ID to this monthly return before saving
            if ($this->MonthlyReturn->save($this->request->data)) {
                $this->Session->setFlash(__('The monthly return has been saved'));
                $this->redirect(array('action' => 'index'));
            } else 
            {
                $this->Session->setFlash(__('The monthly return could not be saved. Please, try again.'));
            }
        }

Any help would be greatly appreciated.

If you define $uses inside your controller you need to explicitly load the MonthlyReturn model:

var $uses = array('MonthlyReturn','Employee','Company');

See documentation

you should load the model first like this

$this->loadModel("MonthlyReturn");

Here the MonthlyReturn is the name of the Model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM