Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get operations name/ type in custom callback? #7056

Open
KiddoV opened this issue Jun 7, 2024 · 0 comments
Open

How to get operations name/ type in custom callback? #7056

KiddoV opened this issue Jun 7, 2024 · 0 comments
Assignees
Labels
type:question general questions

Comments

@KiddoV
Copy link

KiddoV commented Jun 7, 2024

Your Question

I have a custom callback like this:

func customAfterModifyCallback(db *gorm.DB) {
	if db.Error == nil { //This should only happen if GORM successfully make a `commit`
		tbName := db.Statement.Schema.Table
		var rowId any                                      //Row ID should only be a string or a number
		for _, field := range db.Statement.Schema.Fields { //Get model fields
			if field.PrimaryKey {
				rowId, _ = field.ValueOf(db.Statement.Context, db.Statement.ReflectValue)
			}
		}
		fmt.Println(db.RowsAffected, tbName, rowId)
	}
}

//...
if svDB, err := gorm.Open(sqlite.Open(serverDsn), &gorm.Config{SkipDefaultTransaction: true, PrepareStmt: true}); err == nil {
	//
	svDB.Callback().Create().After("gorm:commit").Register("cbf:customAfterModifyCallback", customAfterModifyCallback)
	svDB.Callback().Update().After("gorm:commit").Register("cbf:customAfterModifyCallback", customAfterModifyCallback)
	svDB.Callback().Delete().After("gorm:commit").Register("cbf:customAfterModifyCallback", customAfterModifyCallback)
	//
}

How do I know which operation was use in the callback?
For instance, I want to know if it was INSERT, UPDATE or DELETE operation?

Thanks,

The document you expected this should be explained

N/A

Expected answer

...

@KiddoV KiddoV added the type:question general questions label Jun 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

No branches or pull requests

2 participants