go-ozzo / ozzo-dbx

A Go (golang) package that enhances the standard database/sql package by providing powerful data retrieval methods as well as DB-agnostic query building capabilities.
MIT License
638 stars 91 forks source link

How to make the relationship? #77

Open newerton opened 5 years ago

newerton commented 5 years ago

Hello qiangxue, miss you on the Yii team.

I'm diversifying my portfolio, and improving my clients' APIs, I'm learning Go lang.

I followed several tutorials and blog about relationship, and I am 3 days without being able to make a BELONGS TO relationship.

What am I doing wrong, can you help me?

daos/product.go

func (dao *ProductDAO) Query(rs app.RequestScope, offset, limit int) ([]models.Product, error) {
    products := []models.Product{}
    err := rs.Tx().Select().
                    InnerJoin("establishment", dbx.NewExp("`product`.`establishment_id` = `establishment`.`id`")).
                    OrderBy("product.id").
                    Offset(int64(offset)).
                    Limit(int64(limit)).
                    All(&products)
    return products, err
}

models/product.go

type Product struct {
    Id int                                      `json:"id" db:"id"`
    Establishment       Establishment           `json:"establishment" db:"establishment"`
}

models/establishment.go

type Establishment struct {
    Id int                          `json:"id" db:"id"`
    Subtitle string                 `json:"subtitle" db:"subtitle"`
}