添加钩子函数
添加钩子函数
在插入一条记录到数据库的时候,我希望做点事情
type Student struct {
ID uint `gorm:"size:10"`
Name string `gorm:"size:16"`
Age int `gorm:"size:3"`
Email *string `gorm:"size:128"`
}
func (stu *Student) BeforeCreate(tx *gorm.DB) (err error) {
email := fmt.Sprintf("%s@qq.com", stu.Name)
stu.Email = &email
return nil
}
DB.Create(&Student{})