第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使用 gorm 插件/鉤子將新記錄插入數(shù)據(jù)庫(kù)

如何使用 gorm 插件/鉤子將新記錄插入數(shù)據(jù)庫(kù)

Go
達(dá)令說 2022-07-11 16:26:17
在嘗試使用 golang 中的 gorm 插入日志以檢測(cè)模型的值更改時(shí),我正在使用插件進(jìn)行操作:type MyModel struct { Id Name}type Log struct { Id NewValue OldValue CreatedAt}我的插件定義是這樣的:func ChangelogCreatePlugin(db *gorm.DB) {    log := &Log{NewValue: "the new value", OldValue: "the old value", CreatedAt: "current time"}    // Here is the problem    db.Save(log) <- this is not acceptable}使用插件中的參數(shù)插入不同的數(shù)據(jù)模型db *gorm.DB是不可接受的,因?yàn)樵搮?shù)已初始化為接受來(lái)自觸發(fā)插件的同一模型的數(shù)據(jù)。我的要求是將我的日志存儲(chǔ)在同一個(gè)數(shù)據(jù)庫(kù)事務(wù)中,因此如果其中一個(gè)失敗,它們都應(yīng)該失敗。如何在gorm中做這樣的事情?我知道鉤子。鉤子在我的情況下沒有用,因?yàn)槲蚁M业娜罩靖櫜煌哪P?,所以我正在尋找更多“可重用”的解決方案,而不是在我的模型中復(fù)制/粘貼鉤子實(shí)現(xiàn)。
查看完整描述

3 回答

?
郎朗坤

TA貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個(gè)贊

經(jīng)過大量的挖掘和調(diào)試,我想出了這個(gè)解決方案:


您首先需要以正確的順序注冊(cè)您的插件執(zhí)行,在我的情況下應(yīng)該是這樣的:


gormDb.Callback().Create().Before("gorm:commit_or_rollback_transaction").Register("changelog_create", ChangelogCreatePlugin)

此命令將保證您的插件將在模型的任何插入子句的事務(wù)提交之前被激活或觸發(fā)。


要了解有關(guān)在何處注冊(cè)插件的更多信息,請(qǐng)查看在 gorm 中注冊(cè)的默認(rèn)回調(diào)


這樣做之后,我需要調(diào)整插件代碼:


func ChangelogCreatePlugin(db *gorm.DB) {

    // first make a check that the model insert transaction doesn't have any error

    if db.Error != nil {

        return

    }


    

    log := &Log{NewValue: "the new value", OldValue: "the old value", CreatedAt: "current time"}

    

    // get a new db session for the new model to work

    logDb := db.Session(&gorm.Session{})



    // if an error ocurred while saving the log

    // push it into original model db instance errors, so it will be rolledback eventually

    logErr := logDb.Save(log)

    if logErr != nil {

        db.AddError(logErr)

    }

}

希望有一天這可以幫助某人!


查看完整回答
反對(duì) 回復(fù) 2022-07-11
?
收到一只叮咚

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超5個(gè)贊

嘗試創(chuàng)建事務(wù)性decorator女巫使用func(d *gorm.DB)或f ...func(d *gorm.DB)作為參數(shù)。它應(yīng)該看起來(lái)像


type TransactionalDecorator struct {

    DB *gorm.DB

}


func (t *TransactionalDecorator) Transaction(f ...func(d *gorm.DB) error) error  {

    return t.DB.Transaction(func(tx *gorm.DB) error {

        for _, fn := range f {

            if err := fn(tx); err != nil {

                return err

            }

        }

        return nil

    })

}

所有數(shù)據(jù)庫(kù)交互功能將在同一個(gè)事務(wù)中執(zhí)行


repo , _ := gorm.Open(...)


tdb := &TransactionalDecorator{DB: repo}


saveModel := func(d *gorm.DB) error {

    // save model

}

saveLog := func(d *gorm.DB) error {

    // save log

}


err := tdb.Transaction(saveModel, saveLog)


查看完整回答
反對(duì) 回復(fù) 2022-07-11
?
子衿沉夜

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

怎么樣:


func ChangelogCreatePlugin(db *gorm.DB) {

    log := &Log{NewValue: "the new value", OldValue: "the old value", CreatedAt: "current time"}

    // Here is the problem

    db.Session(&gorm.Session{}).Save(log)

}

這應(yīng)該給你一個(gè)“新鮮”的 db/tx 來(lái)使用


查看完整回答
反對(duì) 回復(fù) 2022-07-11
  • 3 回答
  • 0 關(guān)注
  • 243 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)