Commit 01aa1085 authored by gobenon's avatar gobenon

Merge pull request #1 from gobenon/gobenon-m2mpatch-1

Update orm_querym2m.go
parents a89f14d8 ca37557a
...@@ -44,7 +44,15 @@ func (o *queryM2M) Add(mds ...interface{}) (int64, error) { ...@@ -44,7 +44,15 @@ func (o *queryM2M) Add(mds ...interface{}) (int64, error) {
dbase := orm.alias.DbBaser dbase := orm.alias.DbBaser
var models []interface{} var models []interface{}
last_md_col_name := mi.fields.dbcols[len(mi.fields.dbcols)-1]
last_md := mds[len(mds)-1]
var v3 interface{}
var names []string
var values []interface{}
if reflect.Indirect(reflect.ValueOf(last_md)).Kind() != reflect.Struct {
v3 = (last_md)
mds = mds[:len(mds)-1]
}
for _, md := range mds { for _, md := range mds {
val := reflect.ValueOf(md) val := reflect.ValueOf(md)
if val.Kind() == reflect.Slice || val.Kind() == reflect.Array { if val.Kind() == reflect.Slice || val.Kind() == reflect.Array {
...@@ -63,15 +71,18 @@ func (o *queryM2M) Add(mds ...interface{}) (int64, error) { ...@@ -63,15 +71,18 @@ func (o *queryM2M) Add(mds ...interface{}) (int64, error) {
if exist == false { if exist == false {
panic(ErrMissPK) panic(ErrMissPK)
} }
if v3 != nil {
names = []string{mfi.column, rfi.column, last_md_col_name}
names := []string{mfi.column, rfi.column} values = make([]interface{}, 0, len(models)*3)
} else {
values := make([]interface{}, 0, len(models)*2) names = []string{mfi.column, rfi.column}
values = make([]interface{}, 0, len(models)*2)
}
for _, md := range models { for _, md := range models {
ind := reflect.Indirect(reflect.ValueOf(md)) ind := reflect.Indirect(reflect.ValueOf(md))
var v2 interface{} var v2 interface{}
if ind.Kind() != reflect.Struct { if ind.Kind() != reflect.Struct {
v2 = ind.Interface() v2 = ind.Interface()
...@@ -81,14 +92,17 @@ func (o *queryM2M) Add(mds ...interface{}) (int64, error) { ...@@ -81,14 +92,17 @@ func (o *queryM2M) Add(mds ...interface{}) (int64, error) {
panic(ErrMissPK) panic(ErrMissPK)
} }
} }
if v3 == nil {
values = append(values, v1, v2) values = append(values, v1, v2)
} else {
values = append(values, v1, v2, v3)
}
} }
return dbase.InsertValue(orm.db, mi, true, names, values) return dbase.InsertValue(orm.db, mi, true, names, values)
} }
// remove models following the origin model relationship // remove models following the origin model relationship
func (o *queryM2M) Remove(mds ...interface{}) (int64, error) { func (o *queryM2M) Remove(mds ...interface{}) (int64, error) {
fi := o.fi fi := o.fi
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment