entity feature developing for package gdb

This commit is contained in:
John
2020-06-17 11:37:45 +08:00
parent 386f38af5e
commit 54f47845f6
3 changed files with 71 additions and 10 deletions

View File

@ -158,7 +158,7 @@ func (m *Model) Array(fieldsAndWhere ...interface{}) ([]Value, error) {
// see Model.Where.
//
// Note that it returns sql.ErrNoRows if there's no record retrieved with the given conditions
// from table.
// from table and <pointer> is not nil.
//
// Eg:
// user := new(User)
@ -182,14 +182,14 @@ func (m *Model) Struct(pointer interface{}, where ...interface{}) error {
// see Model.Where.
//
// Note that it returns sql.ErrNoRows if there's no record retrieved with the given conditions
// from table.
// from table and <pointer> is not empty.
//
// Eg:
// users := ([]User)(nil)
// err := db.Table("user").Structs(&users)
// err := db.Table("user").Structs(&users)
//
// users := ([]*User)(nil)
// err := db.Table("user").Structs(&users)
// err := db.Table("user").Structs(&users)
func (m *Model) Structs(pointer interface{}, where ...interface{}) error {
all, err := m.All(where...)
if err != nil {
@ -216,10 +216,10 @@ func (m *Model) Structs(pointer interface{}, where ...interface{}) error {
// err := db.Table("user").Where("id", 1).Struct(&user)
//
// users := ([]User)(nil)
// err := db.Table("user").Structs(&users)
// err := db.Table("user").Structs(&users)
//
// users := ([]*User)(nil)
// err := db.Table("user").Structs(&users)
// err := db.Table("user").Structs(&users)
func (m *Model) Scan(pointer interface{}, where ...interface{}) error {
t := reflect.TypeOf(pointer)
k := t.Kind()

View File

@ -0,0 +1,59 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gdb_test
import (
"fmt"
"testing"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/test/gtest"
)
func Test_Table_Join(t *testing.T) {
var (
tableUser = "user_" + gtime.TimestampMicroStr()
tableUserDetail = "user_detail_" + gtime.TimestampMicroStr()
)
if _, err := db.Exec(fmt.Sprintf(`
CREATE TABLE %s (
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
name varchar(45) NOT NULL COMMENT,
PRIMARY KEY (uid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`, tableUser)); err != nil {
gtest.Error(err)
}
defer dropTable(tableUser)
if _, err := db.Exec(fmt.Sprintf(`
CREATE TABLE %s (
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
name varchar(45) NOT NULL COMMENT,
PRIMARY KEY (uid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`, tableUserDetail)); err != nil {
gtest.Error(err)
}
defer dropTable(tableUserDetail)
gtest.C(t, func(t *gtest.T) {
type EntityUser struct {
Uid int
Name string
}
type EntityUserDetail struct {
Uid int
TrueName string
}
type Entity struct {
User *EntityUser
UserDetail *EntityUserDetail
}
})
}