This commit is contained in:
John Guo
2022-01-15 21:38:15 +08:00
parent 4943c3a9e0
commit d32acabcef
2 changed files with 136 additions and 2 deletions

View File

@ -321,12 +321,18 @@ func (m *Model) Scan(pointer interface{}, where ...interface{}) error {
//
// See Result.ScanList.
func (m *Model) ScanList(structSlicePointer interface{}, bindToAttrName string, relationAttrNameAndFields ...string) (err error) {
var result Result
out, err := checkGetSliceElementInfoForScanList(structSlicePointer, bindToAttrName)
if err != nil {
return err
}
// Filter fields using temporary created struct using reflect.New.
result, err := m.Fields(reflect.New(out.BindToAttrType).Interface()).All()
if m.fields != defaultFields || m.fieldsEx != "" {
// There are custom fields.
result, err = m.All()
} else {
// Filter fields using temporary created struct using reflect.New.
result, err = m.Fields(reflect.New(out.BindToAttrType).Interface()).All()
}
if err != nil {
return err
}

View File

@ -477,6 +477,134 @@ CREATE TABLE %s (
})
}
// https://github.com/gogf/gf/issues/1570
func Test_Table_Relation_Issue1570(t *testing.T) {
var (
tableUser = "user_" + gtime.TimestampMicroStr()
tableUserDetail = "user_detail_" + gtime.TimestampMicroStr()
tableUserScores = "user_scores_" + gtime.TimestampMicroStr()
)
if _, err := db.Exec(ctx, fmt.Sprintf(`
CREATE TABLE %s (
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
name varchar(45) NOT NULL,
PRIMARY KEY (uid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`, tableUser)); err != nil {
gtest.Error(err)
}
defer dropTable(tableUser)
if _, err := db.Exec(ctx, fmt.Sprintf(`
CREATE TABLE %s (
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
address varchar(45) NOT NULL,
PRIMARY KEY (uid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`, tableUserDetail)); err != nil {
gtest.Error(err)
}
defer dropTable(tableUserDetail)
if _, err := db.Exec(ctx, fmt.Sprintf(`
CREATE TABLE %s (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
uid int(10) unsigned NOT NULL,
score int(10) unsigned NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`, tableUserScores)); err != nil {
gtest.Error(err)
}
defer dropTable(tableUserScores)
type EntityUser struct {
Uid int `json:"uid"`
Name string `json:"name"`
}
type EntityUserDetail struct {
Uid int `json:"uid"`
Address string `json:"address"`
}
type EntityUserScores struct {
Id int `json:"id"`
Uid int `json:"uid"`
Score int `json:"score"`
}
type Entity struct {
User *EntityUser
UserDetail *EntityUserDetail
UserScores []*EntityUserScores
}
// Initialize the data.
gtest.C(t, func(t *gtest.T) {
var err error
for i := 1; i <= 5; i++ {
// User.
_, err = db.Insert(ctx, tableUser, g.Map{
"uid": i,
"name": fmt.Sprintf(`name_%d`, i),
})
t.AssertNil(err)
// Detail.
_, err = db.Insert(ctx, tableUserDetail, g.Map{
"uid": i,
"address": fmt.Sprintf(`address_%d`, i),
})
t.AssertNil(err)
// Scores.
for j := 1; j <= 5; j++ {
_, err = db.Insert(ctx, tableUserScores, g.Map{
"uid": i,
"score": j,
})
t.AssertNil(err)
}
}
})
// Result ScanList with struct elements and pointer attributes.
gtest.C(t, func(t *gtest.T) {
var users []Entity
// User
err := db.Model(tableUser).
Where("uid", g.Slice{3, 4}).
Fields("uid").
Order("uid asc").
ScanList(&users, "User")
t.AssertNil(err)
t.AssertNil(err)
t.Assert(len(users), 2)
t.Assert(users[0].User, &EntityUser{3, ""})
t.Assert(users[1].User, &EntityUser{4, ""})
// Detail
err = db.Model(tableUserDetail).
Where("uid", gdb.ListItemValues(users, "User", "Uid")).
Order("uid asc").
ScanList(&users, "UserDetail", "User", "uid:Uid")
t.AssertNil(err)
t.AssertNil(err)
t.Assert(users[0].UserDetail, &EntityUserDetail{3, "address_3"})
t.Assert(users[1].UserDetail, &EntityUserDetail{4, "address_4"})
// Scores
err = db.Model(tableUserScores).
Where("uid", gdb.ListItemValues(users, "User", "Uid")).
Order("id asc").
ScanList(&users, "UserScores", "User", "uid:Uid")
t.AssertNil(err)
t.AssertNil(err)
t.Assert(len(users[0].UserScores), 5)
t.Assert(len(users[1].UserScores), 5)
t.Assert(users[0].UserScores[0].Uid, 3)
t.Assert(users[0].UserScores[0].Score, 1)
t.Assert(users[0].UserScores[4].Score, 5)
t.Assert(users[1].UserScores[0].Uid, 4)
t.Assert(users[1].UserScores[0].Score, 1)
t.Assert(users[1].UserScores[4].Score, 5)
})
}
func Test_Table_Relation_Many_ModelScanList(t *testing.T) {
var (
tableUser = "user_" + gtime.TimestampMicroStr()