This commit is contained in:
John Guo
2021-09-23 19:29:20 +08:00
parent c3b3258194
commit a2c5c577db
4 changed files with 98 additions and 3 deletions

View File

@ -7,6 +7,7 @@
package gdb
import (
"database/sql"
"fmt"
"github.com/gogf/gf/errors/gcode"
"reflect"
@ -156,7 +157,8 @@ func (m *Model) doWithScanStruct(pointer interface{}) error {
model = model.Order(parsedTagOutput.Order)
}
err = model.Fields(fieldKeys).Where(relatedSourceName, relatedTargetValue).Scan(bindToReflectValue)
if err != nil {
// It ignores sql.ErrNoRows in with feature.
if err != nil && err != sql.ErrNoRows {
return err
}
}
@ -265,7 +267,8 @@ func (m *Model) doWithScanStructs(pointer interface{}) error {
err = model.Fields(fieldKeys).
Where(relatedSourceName, relatedTargetValue).
ScanList(pointer, fieldName, parsedTagOutput.With)
if err != nil {
// It ignores sql.ErrNoRows in with feature.
if err != nil && err != sql.ErrNoRows {
return err
}
}

View File

@ -1992,6 +1992,7 @@ PRIMARY KEY (id)
})
}
// https://github.com/gogf/gf/issues/1401
func Test_With_Feature_Issue1401(t *testing.T) {
var (
table1 = "parcels"
@ -2032,3 +2033,65 @@ func Test_With_Feature_Issue1401(t *testing.T) {
t.Assert(parcelDetail.Items[0].ParcelId, 3)
})
}
// https://github.com/gogf/gf/issues/1412
func Test_With_Feature_Issue1412(t *testing.T) {
var (
table1 = "parcels"
table2 = "items"
)
array := gstr.SplitAndTrim(gtest.TestDataContent(`issue1412.sql`), ";")
for _, v := range array {
if _, err := db.Exec(v); err != nil {
gtest.Error(err)
}
}
defer dropTable(table1)
defer dropTable(table2)
gtest.C(t, func(t *gtest.T) {
type Items struct {
gmeta.Meta `orm:"table:items"`
Id int `json:"id"`
Name string `json:"name"`
}
type ParcelRsp struct {
gmeta.Meta `orm:"table:parcels"`
Id int `json:"id"`
ItemId int `json:"item_id"`
Items Items `json:"items" orm:"with:Id=ItemId"`
}
entity := &ParcelRsp{}
err := db.Model("parcels").With(Items{}).Where("id=3").Scan(&entity)
t.AssertNil(err)
t.Assert(entity.Id, 3)
t.Assert(entity.ItemId, 0)
t.Assert(entity.Items.Id, 0)
t.Assert(entity.Items.Name, "")
})
gtest.C(t, func(t *gtest.T) {
type Items struct {
gmeta.Meta `orm:"table:items"`
Id int `json:"id"`
Name string `json:"name"`
}
type ParcelRsp struct {
gmeta.Meta `orm:"table:parcels"`
Id int `json:"id"`
ItemId int `json:"item_id"`
Items Items `json:"items" orm:"with:Id=ItemId"`
}
entity := &ParcelRsp{}
err := db.Model("parcels").With(Items{}).Where("id=30000").Scan(&entity)
t.AssertNE(err, nil)
t.Assert(entity.Id, 0)
t.Assert(entity.ItemId, 0)
t.Assert(entity.Items.Id, 0)
t.Assert(entity.Items.Name, "")
})
}

View File

@ -435,7 +435,6 @@ func Test_Model_Clone(t *testing.T) {
table := createInitTable()
defer dropTable(table)
db.SetDebug(true)
gtest.C(t, func(t *gtest.T) {
md := db.Model(table).Where("id IN(?)", g.Slice{1, 3})
count, err := md.Count()

30
database/gdb/testdata/issue1412.sql vendored Normal file
View File

@ -0,0 +1,30 @@
-- ----------------------------
-- Table structure for items
-- ----------------------------
CREATE TABLE `items` (
`id` int(11) NOT NULL,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of items
-- ----------------------------
INSERT INTO `items` VALUES (1, '金秋产品1');
INSERT INTO `items` VALUES (2, '金秋产品2');
-- ----------------------------
-- Table structure for parcels
-- ----------------------------
CREATE TABLE `parcels` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`item_id` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of parcels
-- ----------------------------
INSERT INTO `parcels` VALUES (1, 1);
INSERT INTO `parcels` VALUES (2, 2);
INSERT INTO `parcels` VALUES (3, 0);