logo

JTBC5.0后台添加或编辑数据前或后处理数据

2025-02-19 点击 52

处理的文件:模块\common\diplomat\manage.php

代码

use Action\Typical\Add;

修改成
use Action\Typical\Add {
actionAdd as private traitActionAdd;
}

添加代码块

  public function actionAdd(Request $req)
  {
    $this -> hook -> afterAutoSave = function($argId)
      {
        $id = intval($argId);
        $accountId = $this -> guard -> account -> id;
        $model = new TinyModel();
        $model -> where -> id = $id;
        $model -> pocket -> account_id = $accountId;
        $model -> save();
     };
     return $this -> traitActionAdd($req);
  }

  public function actionAdd(Request $req)
  {
    $this -> hook -> beforeAutoSave = function(Request $req)
    {
      $result = null;
      $id = intval($req -> get('id'));
      $name = strval($req -> post('name'));
      if (!Validation::isEmpty($name))
      {
        $model = new TinyModel();
        $model -> where -> name = $name;
        $rs = $model -> get();
        if (!is_null($rs))
        {
          if ($rs -> id != $id)
          {
            $result = new ErrorCollector();
            $result -> collect(['code' => 4001, 'message' => Jtbc::take('manage.text-code-4001', 'lng')]);
          }
        }
      }
      return $result;
    };
     return $this -> traitActionAdd($req);
  }


这里是处理添加数据时,如果是编辑数据时,可以对应的修改即可。

use Action\Typical\Edit {
actionEdit as private traitActionEdit;
}

public function actionEdit(Request $req)
{

//这是处理代码块

return $this -> traitActionEdit($req);

}

0%