Golang Gorm Fiber – 我如何将我定义的别名发送到我的索引模板中?

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

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, 
})

然后,我将我写的名称作为索引模板别名输入,它给出了输出 Golang Gorm Fiber – 我如何将我定义的别名发送到我的索引模板中?

英文:

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 Golang Gorm Fiber – 我如何将我定义的别名发送到我的索引模板中?

huangapple
  • 本文由 发表于 2023年8月9日 07:02:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76863640.html
匿名

发表评论

匿名网友

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

确定