如何存储来自DTO Gorm的数据

huangapple go评论83阅读模式
英文:

How do I store the data from DTO Gorm

问题

我正在使用golang的Fiber和GORM。我试图将数据从DTO存储起来。例如,我创建了一个名为"User"的实体和一个名为"PostDto"的DTO,如下所示:

type User struct {
    gorm.Model
    Name    string
    Address string
    Phone   string
}

type PostDto struct {
    Name    string `json:"name"`
    Address string `json:"address"`
    Phone   string `json:"phone"`
}

我试图按照以下方式存储数据:

var postDto models.PostDto
c.BodyParser(&postDto)
err := database.DB.Model(models.User{}).Create(&postDto).Error
fmt.Println(err.Error())

但是我得到了以下错误:

panic: reflect: call of reflect.Value.Field on string Value

有人可以帮助解决这个问题吗?

提前感谢!

英文:

I am using golang fiber and GORM. I am trying to store the data from DTO. For example, I have created a post entity and post dto like below.

type User struct {
    gorm.Model
	Name string
    Address string
    Phone string
}

This is my DTO

type PostDto struct {
	Name string `json:"name"`
    Address string `json:"address"`
    Phone string `json:"phone"`
}

I am trying to store the data like below

var postDto models.PostDto
c.BodyParser(&postDto)
err := database.DB.Model(models.User{}).Create(&postDto).Error
fmt.Println(err.Error())

But I am getting the below error

panic: reflect: call of reflect.Value.Field on string Value

Could anyone help to resolve this issue?

Thanks in advance

答案1

得分: 4

我不知道你在做什么?这是Go语言,不是Java,为什么要定义两个结构体?

你可以使用标签来完成你想要的。

type User struct {
    gorm.Model `json:"-" gorm:"-"`
    Name string `json:"name" gorm:"column:name"`
    Address string `json:"address" gorm:"column:address"`
    Phone string `json:"phone"  gorm:"column:phone"`
}


var user User
err := database.DB.Model(User{}).Create(&user).Error
fmt.Println(err.Error())
英文:

I don't know what you are doing? this is go language, not java, why do you define two structs?

You can use tag to complete that you want.

type User struct {
    gorm.Model `json:"-" gorm:"-"`
    Name string `json:"name" gorm:"column:name"`
    Address string `json:"address" gorm:"column:address"`
    Phone string `json:"phone"  gorm:"column:phone"`
}


var user User
err := database.DB.Model(User{}).Create(&user).Error
fmt.Println(err.Error())

答案2

得分: 1

用户和PostDto结构不同,使用一个模型从另一个模型获取和保存数据是错误的。

要么创建一个函数将PostDto转换为User,并在Create中使用其输出:

func (postDTo PostDTo) ToUser() *User {
    return &User{Name: postDTo.Name, Address: postDTo.Address, Phone: postDTo.Phone}
}

要么,由于User和PostDto结构非常相似,在User的gorm.Model中添加json ignore,并使用它代替PostDto:

type User struct {
    gorm.Model `json:"-"`
    Name       string `json:"name"`
    Address    string `json:"address"`
    Phone      string `json:"phone"`
}

var postData models.User
c.BodyParser(&postData)
英文:

User and PostDto structures are not same, using one model to get and save data from another is wrong.

Either create a function to convert from PostDto to User, adn use its output in Create

func (postDTo PostDTo) ToUser() *User {
	return &User{Name: postDTo.Name, Address: postDTo.Address, Phone: postDTo.Phone}
}

OR, as User and PostDto structs are so similar, add json ignore to gorm.Model in User and use it instead of PostDto

type User struct {
	gorm.Model `json:"-"`
	Name       string `json:"name"`
	Address    string `json:"address"`
	Phone      string `json:"phone"`
}


var postData models.User
c.BodyParser(&postData)

huangapple
  • 本文由 发表于 2022年8月31日 13:40:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/73551307.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定