[英]Fatal error: Call to a member function save() on a non-object cakephp
I'm new to cakephp, so I got this error message: 我是cakephp的新手,所以我收到了以下错误消息:
http://imageshack.com/a/img716/3163/ofvq.png http://imageshack.com/a/img716/3163/ofvq.png
this is my CalaboradoresController.php file: 这是我的CalaboradoresController.php文件:
<?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: 这是我的Colaborador.php文件:
<?php
class Colaborador extends AppModel{
public $name = 'Colaborador';
}
?>
Change you controller filename to ColaboradorsController.php and controller name ColaboradorsController 将控制器文件名更改为ColaboradorsController.php和控制器名称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 模型必须有一个类名,例如admin,初始化的var
$name
将是你的表
so when your trying to save data you must refer the model name to $this->admin->save($this->data)
. 因此,当您尝试保存数据时,必须将型号名称引用到
$this->admin->save($this->data)
。
This will save from error 这样可以避免错误
You need to load the Model first before calling the save function. 在调用save函数之前,需要先加载Model。
you can load the Model in 2 ways 您可以通过两种方式加载模型
First : Define $uses inside your controller you need to explicitly load the Colaborador model: 首先:在控制器中定义$ uses,您需要显式加载Colaborador模型:
var $uses = array('Colaborador','MonthlyReturn','Employee','Company');
Second : You can use loadModel function like this 第二:你可以像这样使用loadModel函数
$this->loadModel('Colaborador');
Use this loadModel function before save function. 在保存功能之前使用此loadModel函数。
I hope this will be helpful for you. 我希望这对你有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.