【问题标题】:Yii2 Duplication of Model - rowYii2复制模型 - 行
【发布时间】:2019-01-25 18:57:23
【问题描述】:

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

我的代码如下

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】:

    您的问题过于宽泛,您没有提到您当前面临的具体问题,但考虑到您是社区新手,我会尝试以您可以相应翻译的方式回答。

    最重要的是,您描述的问题需要在您的模型的 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)

    我将在我的Books 模型中添加以下beforeSave() 函数

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

    【讨论】:

    • 非常感谢。你已经很清楚我的要求了。简而言之,我的要求是在不为我的应用程序使用扩展的情况下保留我的记录和审计跟踪的历史记录。我会尽快尝试您的代码。阅读您的内容后,我相信它会起作用。我在 aftersave() 函数中编写我的代码。非常感谢。
    • 嗨 Muhammad Omer Aslam 我发现了一个问题。- 如果 Book 有一个 PK,那么我添加了一行 $book->id = null;否则将是违规错误。否则作为一种魅力。希望我可以将此解决方案扩展到我的相关表。谢谢
    • 抱歉,但我无法确切了解您遇到了什么问题,我最终测试了这个解决方案,它可以正常工作,没有任何问题,@IgnatiusAbraham
    • 如果答案对您有用,请将其标记为正确@IgnatiusAbraham
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多