英文:
Golang Gorm Fiber - How can I send the name I defined as alias to my index template?
问题
我正在尝试从PHP切换到GO,但在一个地方卡住了,我请求你的帮助。
我刚刚在查询中定义了一个名为"durums"的别名,但数据库中没有这个字段。我该如何将我定义的别名发送到我的索引模板?
Vilan := []model.Vfilan{}
dSubquery := r.DB.Table("Dipfilan").
Select("(select id from iform where id = Dipfilan.id and actv='1' )as uuid ").
Where("tur = '2' AND finish > ?", time.Now())
errIlan := r.DB.Table("Vfilan").
Select("id, baslik,durumid, " +
"(SELECT title FROM idurum WHERE idurum.id = Vfilan.durumid) as durums").
Where("onay = '1' AND (id IN ? )", dSubquery).
Order("id DESC").
Find(&Vilan).
Error
if errIlan != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": "err" + errIlan.Error(),
})
}
return c.Render("index", fiber.Map{
"VindwIlan": Vilan,
"Vdurum": durums,
})
> "Vdurum": durums, 我得到了未声明的名称错误:durums
我该如何将"durums"的内容发送到我的索引模板?
你能分享完整的错误信息吗?
未声明的名称错误:durums
你能具体说明在哪一行出现了这个错误吗?
return c.Render("index", fiber.Map{
"VindwIlan": Vilan,
"Vdurum": durums,
})
英文:
I'm trying to switch from PHP to GO, but I'm stuck at one point, I ask for your help.
I just define an alias named "durums" in the query, there is no such named field in the database. how can I send the name I defined as alias to my index template?
Vilan := []model.Vfilan{}
dSubquery := r.DB.Table("Dipfilan").
Select("(select id from iform where id = Dipfilan.id and actv='1' )as uuid ").
Where("tur = '2' AND finish > ?", time.Now())
errIlan := r.DB.Table("Vfilan").
Select("id, baslik,durumid, "+
"(SELECT title FROM idurum WHERE idurum .id = Vfilan.durumid) as durums").
Where("onay = '1' AND (id IN ? )", dSubquery).
Order("id DESC").
Find(&Vilan).
Error
if errIlan != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": "err" + errIlan.Error(),
})
}
return c.Render("index", fiber.Map{
"VindwIlan": Vilan,
"Vdurum": durums,
})
> "Vdurum": durums, I get the error undeclared name: durums
How can I send the "durums" content to my index template.
Can you share the full error message?
undeclared name: durums
Can you specify on which line you are getting this error?
return c.Render("index", fiber.Map{
"VindwIlan": Vilan,
"Vdurum": durums,
})
答案1
得分: 0
我解决了我的问题。
我实现了以下的结构。需要的人可以从这里复制。
首先,我定义了一个新的结构,并在其中添加了我的主表,然后我将作为别名提取的字段添加到其中。
type Vilanlar struct {
model.Vfilan
Durums string `gorm:"->" json:"durums"`
}
然后,我编写了我的代码 - 更新了"errIlanlar := r.DB.Model(&model.Vfilan{})"
Vilan := []Vilanlar{}
dSubquery := r.DB.Table("Dipfilan").
Select("(select id from iform where id = Dipfilan.id and actv='1' )as uuid ").
Where("tur = '2' AND finish > ?", time.Now())
errIlanlar := r.DB.Model(&model.Vfilan{}).
Select("id, baslik,durumid, " +
"(SELECT title FROM idurum WHERE idurum.id = Vfilan.durumid) as durums").
Where("onay = '1' AND (id IN ? )", dSubquery).
Order("id DESC").
Find(&Vilan).
Error
if errIlan != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": "err" + errIlan.Error(),
})
}
return c.Render("index", fiber.Map{
"VindwIlan": Vilan,
})
然后,我将我写的名称作为索引模板别名输入,它给出了输出
英文:
I solved my problem.
I implemented a structure like below. Anyone who needs it can copy it from here.
> First i define a new struct and I added my main table in it
and then I added the fields that I pulled as alias under it.
type Vilanlar struct {
model.Vfilan
Durums string `gorm:"->" json:"durums"`
}
> then i write my code - update "errIlanlar := r.DB.Model(&model.Vfilan{}) "
Vilan := []Vilanlar{}
dSubquery := r.DB.Table("Dipfilan").
Select("(select id from iform where id = Dipfilan.id and actv='1' )as uuid ").
Where("tur = '2' AND finish > ?", time.Now())
errIlanlar := r.DB.Model(&model.Vfilan{}).
Select("id, baslik,durumid, "+
"(SELECT title FROM idurum WHERE idurum .id = Vfilan.durumid) as durums").
Where("onay = '1' AND (id IN ? )", dSubquery).
Order("id DESC").
Find(&Vilan).
Error
if errIlan != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": "err" + errIlan.Error(),
})
}
return c.Render("index", fiber.Map{
"VindwIlan": Vilan,
})
> and - I typed the name I wrote as index template alias and it gave output
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论