tp6,定義新增修改方法在模型中
在tp51中,直接使用self::save()表示新增
但是在tp6裡面,使用self::save(),報錯,並不是一個靜態方法
Non-static method think\Model::save() should not be called statically
如果使用$this,報錯
Using $this when not in object context
那麼怎麼才能在模型中呼叫父級的方法實作新增呢?
<?php namespace app\common\model; use think\Model; use think\exception\PDOException; class Common extends Model { // protected static function init(): void { } /** * 添加修改 **/ public static function addEdit($data = []){ $type = isset($data['id']) ? ($data['id']>0 ? 2 : 1) : 1; //?? ?: try { if($type == 2){ //更新 $row = self::update($data); }else{ $row = self::save($data); //$row = $this->save($data); } if($row !==false){ return ['status'=>1,'msg'=>'操作成功', 'data' => '' ]; }else{ return ['status'=>0,'msg'=>'操作失败', 'data' => '' ]; } } catch (PDOException $e) { return ['status'=>0,'msg'=>$e->getMessage()]; } }
朝游东海2019-04-24 11:07:32
已解決,自己的問題
<?php namespace app\common\model; use think\Model; use think\exception\PDOException; class Common extends Model { // protected static function init(): void { } /** * 添加修改 **/ public function addEdit(array $data = []){ //去掉static静态声明 $type = isset($data['id']) ? ($data['id']>0 ? 2 : 1) : 1; //?? ?: try { if($type == 2){ //更新 $row = self::update($data); }else{ $row = $this->save($data); } if($row !==false){ return ['status'=>1,'msg'=>'操作成功', 'data' => '' ]; }else{ return ['status'=>0,'msg'=>'操作失败', 'data' => '' ]; } } catch (PDOException $e) { return ['status'=>0,'msg'=>$e->getMessage()]; } }
能呼叫save()方法了,但是
又出錯了
array_merge(): Argument #1 is not an array
$field = array_merge($this->field, $append);
列印$this->field
#eq true
##列印$this->field
???右鍵,close all,######等正式發布以後再來###