tp6, how to call the inherited save method in the model to add new data?
朝游东海
朝游东海 2019-04-24 10:15:03
0
1
2496

tp6, define new modification methods in the model
In tp51, directly use self::save() to indicate new additions
But in tp6, use self::save(), report an error, and Not a static method
Non-static method think\Model::save() should not be called statically
If you use $this, an error will be reported
Using $this when not in object context
Then how can How about calling the parent's method in the model to add it?

<?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()];
        }
    }


朝游东海
朝游东海

要成为大佬

reply all(1)
朝游东海

Solved, my problem

<?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()];
        }
    }

I can call the save() method, but

An error occurred again

array_merge(): Argument #1 is not an array

$field = array_merge($this->field, $append);

Print $this->field

eq true

???

Right click, close all,

Come back after the official release

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template