// Model specify the model you would like to run db operations // // update all users's name to `hello` // db.Model(&User{}).Update("name", "hello") // // if user's primary key is non-blank, will use it as condition, then will only update the user's name to `hello` // db.Model(&user).Update("name", "hello") func(s *DB)Model(value interface{}) *DB { c := s.clone() // 从这里调用 c.Value = value return c }
接着走, 在获取表名的时候,需要调用scope.db.Model, 这里的 db 应该是一个nil导致调用失败
funcconvertInterfaceToMap(values interface{}, withIgnoredField bool)map[string]interface{} { var attrs = map[string]interface{}{}
switch value := values.(type) { casemap[string]interface{}: return value case []interface{}: for _, v := range value { for key, value := range convertInterfaceToMap(v, withIgnoredField) { attrs[key] = value } } caseinterface{}: reflectValue := reflect.ValueOf(values)
switch reflectValue.Kind() { case reflect.Map: for _, key := range reflectValue.MapKeys() { attrs[ToDBName(key.Interface().(string))] = reflectValue.MapIndex(key).Interface() } default: // 在这里调用 for _, field := range (&Scope{Value: values}).Fields() { if !field.IsBlank && (withIgnoredField || !field.IsIgnored) { attrs[field.DBName] = field.Field.Interface() } } } } return attrs }
// GetModelStruct get value's model struct, relationships based on struct and tag definition func(scope *Scope)GetModelStruct() *ModelStruct { //... // Get Cached model struct if value := modelStructsMap.Get(reflectType); value != nil { return value } // ... }