logo

JTBC5.0模块中特殊数据的处理方法

2025-02-19 点击 52

JTBC5.0模块中特殊数据的处理方法

解释一下,什么叫特殊数据,比如我录入一条新闻,要记录对应的录入人账号。这个在录入数据时,通过代码自动识别,无感自动后台录入。即不用人工选择录入人姓名。

JTBC5.0默认是自动化处理字段的,所以在入库代码中操作比较麻烦。

但JTBC5.0提供了钩子函数afterAutoSave,可以实现在保存数据后再进行一些操作。

我们就通过这个钩子来实现。

比如以下几个

说明:
        这里的public function __start()是添加或编辑数据时都会执行。如果要分开来执行不同代码,可以参考《JTBC5.0后台添加或编辑数据前或后处理数据》

录入人账号ID

  public function __start()
  {
    $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();
    };
  }


配置信息的录入

  public function __start()
  {
    $this -> hook -> afterAutoSave = function($argId)
    {
      $id = intval($argId);
      $siteid = Jtbc::take('global.' . $this -> getParam('genre') . ':config.siteid', 'cfg');
      $model = new TinyModel();
      $model -> where -> id = $id;
      $model -> pocket -> site = $siteid;
      $model -> save();
    };
  }


处理其它数据

    $this -> hook -> afterAutoSave = function($argId)
    {
      $id = intval($argId);
      $model = new TinyModel();
      $model -> where -> id = $id;
      $rs = $model -> get();
      if (!is_null($rs))
      {
        Dictionary::refresh($rs -> name);
      }
    };


同步更新标签模块数据

  public function __start()
  {
    $this -> hook -> afterAutoSave = function($argId)
    {
      $id = intval($argId);
      $genre = $this -> getParam('genre');
      $lang = $this -> guard -> role -> getLang();
      $tagManager = new TagManager($genre, $lang, $id);
      $tagManager -> update($this -> di -> request -> post('tag'));
    };
  }
0%