mirror of
https://gitee.com/johng/gf
synced 2026-07-06 21:45:34 +08:00
fix issue #1387
This commit is contained in:
@ -209,7 +209,12 @@ func ConvertDataForTableRecord(value interface{}) map[string]interface{} {
|
||||
data[k] = nil
|
||||
}
|
||||
|
||||
case *time.Time, *gtime.Time:
|
||||
case *gtime.Time:
|
||||
if r.IsZero() {
|
||||
data[k] = nil
|
||||
}
|
||||
|
||||
case *time.Time:
|
||||
continue
|
||||
|
||||
case Counter, *Counter:
|
||||
|
||||
@ -83,8 +83,10 @@ func (m *Model) Data(data ...interface{}) *Model {
|
||||
list[i] = ConvertDataForTableRecord(rv.Index(i).Interface())
|
||||
}
|
||||
model.data = list
|
||||
|
||||
case reflect.Map:
|
||||
model.data = ConvertDataForTableRecord(data[0])
|
||||
|
||||
case reflect.Struct:
|
||||
if v, ok := data[0].(apiInterfaces); ok {
|
||||
var (
|
||||
@ -98,6 +100,7 @@ func (m *Model) Data(data ...interface{}) *Model {
|
||||
} else {
|
||||
model.data = ConvertDataForTableRecord(data[0])
|
||||
}
|
||||
|
||||
default:
|
||||
model.data = data[0]
|
||||
}
|
||||
|
||||
@ -3871,3 +3871,44 @@ func Test_Model_FieldAvg(t *testing.T) {
|
||||
t.Assert(all[0]["total"], 1)
|
||||
})
|
||||
}
|
||||
|
||||
// https://github.com/gogf/gf/issues/1387
|
||||
func Test_Model_GTime_DefaultValue(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type User struct {
|
||||
Id int
|
||||
Passport string
|
||||
Password string
|
||||
Nickname string
|
||||
CreateTime *gtime.Time
|
||||
}
|
||||
data := User{
|
||||
Id: 1,
|
||||
Passport: "user_1",
|
||||
Password: "pass_1",
|
||||
Nickname: "name_1",
|
||||
}
|
||||
// Insert
|
||||
_, err := db.Model(table).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Select
|
||||
var (
|
||||
user *User
|
||||
)
|
||||
err = db.Model(table).Scan(&user)
|
||||
t.AssertNil(err)
|
||||
t.Assert(user.Passport, data.Passport)
|
||||
t.Assert(user.Password, data.Password)
|
||||
t.Assert(user.CreateTime, data.CreateTime)
|
||||
t.Assert(user.Nickname, data.Nickname)
|
||||
|
||||
// Insert
|
||||
user.Id = 2
|
||||
_, err = db.Model(table).Data(user).Insert()
|
||||
t.AssertNil(err)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user