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);
    }
4

1 回答 1

0

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

最重要的是,您描述的问题需要在您的模型中实现,该模型beforeSave()在插入或更新记录开始时被调用,而不是afterSave()因为您的记录已经用新值更新并且您绝对不想这样做.

根据您的要求。

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

因此,当现有记录的属性发生变化时,主要有 3 件事

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

我不会添加实际代码,因为没有太多关于交互模型的可用信息,因此查看需求我将使用假设我有书的场景以及每当书名发生更改或更新时它应该使用新值添加一条新记录,并将旧记录保留为将status列更改为,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;
}
于 2018-08-19T19:09:38.677 回答