>  Q&A  >  본문

tp6, 새 데이터를 추가하기 위해 모델에서 상속된 저장 메소드를 호출하는 방법은 무엇입니까?

tp6, 모델에 새로운 수정 방법 정의
tp51에서는 self::save()를 직접 사용하여 새로운 추가를 표시합니다
그러나 tp6에서는 self::save()를 사용하면 오류가 보고되며 이는 정적 메서드가 아닙니다.
비정적 메서드 thinkModel::save()는 정적으로 호출하면 안 됩니다
$this를 사용하면 오류가 보고됩니다
객체 컨텍스트가 아닐 때 $this를 사용하면
그러면 모델의 상위 메서드를 어떻게 호출하여 추가해?

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


朝游东海朝游东海1978일 전2453

모든 응답(1)나는 대답할 것이다

  • 朝游东海

    朝游东海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(): 인수 #1은 배열이 아닙니다

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

    Print $this->field

    eq true

    ???

    오른쪽 클릭, 모두 닫기

    공식 출시 후 돌아오세요

    회신하다
    0
  • 취소회신하다