Yii2模型重复 - 行

问题描述 投票:1回答:1

在编辑或更新模型属性时,我不希望更新该记录。而应创建一个新记录,并禁用旧记录。我还有另一个日志表,其中保存旧记录。

我的代码如下

public function afterSave($insert, $changedAttributes)
{


  if ($insert) {
           // Да это новая запись (insert)
            $model = new Test3log();
            $model->desc = $this->desc ;
            $model->id_old = $this->id;
            $model->isdisabled=1;
            $model->save();
        } else {

            $save = "";
            foreach ($changedAttributes as $change => $val) {
                if (trim($val) != trim($this->{$change})) {
                    $save .= $this->attributeLabels()[$change] . '[' . $val . '->' . $this->{$change} . "]\n";
                }
            }
            $xx =$this->getoldattributes();
            if ($save != "") {
                 //  Get Old data
                 // Get New data
                 // repl new record with old id
                 // repl old record with new id
                $modelnewline = new Test3();
                $modelnewline->desc = $xx['desc'];
                $modelnewline->id_old = $xx['id'];
                $modelnewline->id = NULL;
                $modelnewline->isdisabled = 1;
                $modelnewline->save();
                $newid = $modelnewline->id;
                $oldid =$this->id;
                $this->isdisabled=1;
                $this->id = $newid;
                $this->desc = $changedAttributes['desc'];
                $this->save(false);

             }
        }
        parent::afterSave($insert, $changedAttributes);
    }
logging yii2 save audit trail
1个回答
0
投票

你的问题太宽泛了,你还没有提到你目前面临的问题究竟是什么,但考虑到你是社区的新手,我会尝试以一种你可以相应翻译的方式回答它。

最重要的是,您描述的问题需要在模型的beforeSave()中实现,该afterSave()在插入或更新记录开始时调用,而不是status,因为您的记录已经使用新值更新,您绝对不希望去做。

根据您的要求。

在编辑或更新模型属性时,我不希望更新该记录。相反,应创建新记录并禁用旧记录。另外,我有另一个日志表,其中保存旧记录。

因此,当现有记录的属性发生变化时,有三个主要内容

  • 通过将状态更新为0来禁用当前记录。
  • 添加包含新值的新记录。
  • 使用旧值在备份或日志表中添加新记录。

我不会添加实际的代码,因为没有太多关于什么模型进行交互的信息,所以我将使用我将假设我有书籍以及每当书名更改或更新时的情况它应该使用新值添加新记录并保留旧记录,将0列更改为BooksBackup,以便禁用该书并将旧值备份到Books表。所以基本上你会有一个线框来相应地调整你的代码。

您可以根据您使用的模型使用逻辑。

下面是示例模式

  • name varchar(255)模型 status tinyint(1) BooksBackup
  • id int(11)模型 book_id int(11) name varchar(255) beforeSave()

我将在我的Books模型中添加以下public function beforeSave($insert) { if( !parent::beforeSave($insert) ){ return false; } //your custom code if( !$insert ){ $ifAttributesChanged = (!empty($this->dirtyAttributes) ); //if attributes were changed if( $ifAttributesChanged ){ //add new record with the new values $book = new self(); $book->attributes = $this->attributes; $book->save(); //add back the old values of the changed attributes //to the current record so nothing is changed for the current record foreach( $this->dirtyAttributes as $attribute => $value ){ $this->$attribute = $this->oldAttributes[$attribute]; } //disable the current record $this->status = 0; //backup old record $backup = new BackupBooks(); $backup->book_id = $this->id; $backup->name = $this->name; $backup->save(); } } return true; } 函数

qazxswpoi
© www.soinside.com 2019 - 2024. All rights reserved.