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

问题描述 投票:0回答:1

我正在尝试从 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, 
})
go go-gorm go-templates go-fiber go-html-template
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, 
    })


并且 - 我输入了作为索引模板别名编写的名称,它给出了输出:)

© www.soinside.com 2019 - 2024. All rights reserved.