简体   繁体   中英

Fatal error: Call to a member function save() on a non-object cakephp

I'm new to cakephp, so I got this error message:

http://imageshack.com/a/img716/3163/ofvq.png

this is my CalaboradoresController.php file:

<?php

class ColaboradoresController extends AppController{

    public $name = 'Colaboradores';

    public function add(){
        if($this->data){
            if($this->Colaborador->save($this->data))
                $this->Session->setFlash ('Colaborador adicionado com sucesso!');
        }
    }

}

?>

And this is my Colaborador.php file:

<?php

    class Colaborador extends AppModel{

        public $name = 'Colaborador';

    }

?>

Change you controller filename to ColaboradorsController.php and controller name ColaboradorsController

Or you could just add this to you controller:

public $uses = 'Colaborador';

Model must have a class name eg admin and the var $name initialized will be your table

so when your trying to save data you must refer the model name to $this->admin->save($this->data) .

This will save from error

You need to load the Model first before calling the save function.

you can load the Model in 2 ways

First : Define $uses inside your controller you need to explicitly load the Colaborador model:

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

Second : You can use loadModel function like this

 $this->loadModel('Colaborador');

Use this loadModel function before save function.

I hope this will be helpful for you.

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