1、ThinkPHP5.1模型save方法
Thinkphp5.1模型中报错Using $this when not in object context
Using $this when not in object context的出现原因是因为在静态方法中使用$this或者直接调用非静态的方法。
错误代码
//thinkphp 模型类 /* * 新增一条数据 */ public static function addBlog($post) { return $this->save($post); }
服务层调用
/** * 创建资源 * * @return \think\Response */ public function create($post) { $result = Blog::addBlog($post); return $result; }
解决办法1(在模型方法中实例化Blog模型在调用save)
/* * 新增一条数据 */ public static function addBlog($post) { $blog = new Blog(); return $blog->save($post); }
解决办法2
a、addBlog()采用非静态方法可以用$this:public function addBlog($post)
b、addBlog()采用非静态方法可以用$this:public function addBlog($post)
/* * 新增一条数据 */ public function addBlog($post) { return $this->save($post); }
服务层调用(更新为)
/** * 创建资源 * * @return \think\Response */ public function create($post) { $blog = new Blog(); $result = $blog->addBlog($post); return $result; }
发表评论 取消回复