These are chat archives for gin-gonic/gin

11th
May 2017
Rosa Imantoro
@rimantoro
May 11 2017 12:20
is there any suggestion for ORM that close to PHP Eloquent ? and is there any sample code for how to implement it ?
I’m currently new in Go
Gergely Brautigam
@Skarlso
May 11 2017 12:33
does this suit your need??
Rosa Imantoro
@rimantoro
May 11 2017 12:34
@Skarlso yeah I’m actually tried to implement it, a bit less but it the most close
trying to separate each model into single model file but not in success yet
Gergely Brautigam
@Skarlso
May 11 2017 12:36
Mmmmm... Ok?
Thomas Boerger
@tboerger
May 11 2017 12:53
i’m switching to xorm instead of gorm...
Rosa Imantoro
@rimantoro
May 11 2017 12:58
@tboerger why ? gorm looks have more features
@Skarlso still not success, I dont now how to correctly import the model file from a sub folder on my main.go, do you know any example script ?
Andrey Nering
@andreynering
May 11 2017 13:01
@rimantoro You need to setup and develop in your GOPATH: https://golang.org/doc/code.html
Gorm is way more stable than Xorm IMHO
Rosa Imantoro
@rimantoro
May 11 2017 13:04
@andreynering
already set. I’m trying to use this simple scaffold actually
— main.go
— models/
—— client.go
Andrey Nering
@andreynering
May 11 2017 13:07
You should put everything on a $GOPATH/myapp folder.
Than you can import myapp/models
Rosa Imantoro
@rimantoro
May 11 2017 13:15
I put my codes in $GOPATH/src/github.com/rimantoro/gorest this suppose to be the same with your suggestion
and this is my client.go model
package models

import (
    "time"
)

type Client struct {
    ID           uint   `gorm:"primary_key"`
    ClientID     string `gorm:"column:client_id; type:varchar(100); unique; not_null" json:"client_id"`
    ClientName   string `gorm:"column:client_name; type:varchar(100); not_null" json:"client_name"`
    ClientSecret string `gorm:"column:client_secret; type:varchar(100); not_null" json:"client_secret"`
    // Type             string        `gorm:"column:type"`
    // StakeholderID     int            `gorm:"column:stakeholder_id"`
    ClientActive bool `gorm:"column:client_active; type:boolean; default:true" json:"client_active"`

    CreatedAt time.Time  `gorm:"column:created_at; type:timestamp" json:"created_at"`
    UpdatedAt time.Time  `gorm:"column:updated_at; type:timestamp" json:"updated_at"`
    DeletedAt *time.Time `gorm:"column:deleted_at; type:timestamp" json:"deleted_at"`
}

func (Client) TableName() string {
    return "clients"
}
I’m importing this on my main.go
"github.com/rimantoro/gorest/models”
Thomas Boerger
@tboerger
May 11 2017 13:18
@rimantoro where? xorm supports more databases :)
and it got better join queries
Rosa Imantoro
@rimantoro
May 11 2017 13:18
But always throw “undefined: Client” in main function
func main() {
    router := gin.Default()

    // Do the migrations
    db := Database()
    defer db.Close()
    db.AutoMigrate(&Client{})

    v1 := router.Group("/api/v1/client")
    {
        v1.POST("/list", GetList)
        v1.GET("/:id", GetById)
        v1.POST("/", AddNew)
        v1.PUT("/:id", DoUpdate)
        v1.DELETE("/:id", DoDelete)
    }

    router.Run(":8000")
}
Thomas Boerger
@tboerger
May 11 2017 13:18
models.Client
Rosa Imantoro
@rimantoro
May 11 2017 13:19
at this line
db.AutoMigrate(&Client{})
Thomas Boerger
@tboerger
May 11 2017 13:19
yes
Client is part of the models package
i prefer singluar for the model package
Rosa Imantoro
@rimantoro
May 11 2017 13:23
:smile: yes it s work
nubie mistake
@tboerger gorm doesnt have support many DB as xorm, but its support the majority of common DB stack ( MySQL, SQLite, SQL Server & Postgre )
Andrey Nering
@andreynering
May 11 2017 13:27
@tboerger Both have features the other don't support. Xorm doesn't support preloading