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

gorm gen 生成的 entity, 应该怎么写 hooks 呢? #7161

Open
seth-shi opened this issue Aug 20, 2024 · 8 comments
Open

gorm gen 生成的 entity, 应该怎么写 hooks 呢? #7161

seth-shi opened this issue Aug 20, 2024 · 8 comments
Assignees
Labels

Comments

@seth-shi
Copy link

  • 用了 gen 之后现在生成的目录如下
  • do
  • entity
  • 如果我要写 hooks, 怎么在 entity 目录之外写 hooks 呢
@seth-shi seth-shi added the type:question general questions label Aug 20, 2024
@github-actions github-actions bot added type:missing reproduction steps missing reproduction steps and removed type:question general questions labels Aug 20, 2024
@seth-shi
Copy link
Author

我是要针对 entity 下的某个模型, 创建的时候要生成 uuid

Copy link

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

@kuangshp
Copy link

我是要针对 entity 下的某个模型, 创建的时候要生成 uuid

你那边处理了吗,我也遇到这样的,但是最后用全局插件写的

@seth-shi
Copy link
Author

没找到解决方法

Copy link

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

@paradox-lab2
Copy link

这在go语言层面上就不支持实现吧,结构体方法只能定义在同一个包内

@seth-shi
Copy link
Author

这在go语言层面上就不支持实现吧,结构体方法只能定义在同一个包内

很多种方式可以实现, 比如提供一个注册方法

gorm.RegisterHooks(&Model{}, "created", func (m Model) {
      // xxxx
})


gorm.RegisterHooks(&Model{}, func (m Model) {
      m.Saving(function () {
             // xxxx
       })
})

Copy link

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants