diff --git a/database/gdb/gdb_model_order_group.go b/database/gdb/gdb_model_order_group.go index 7922c27f9..9d33cc46d 100644 --- a/database/gdb/gdb_model_order_group.go +++ b/database/gdb/gdb_model_order_group.go @@ -15,8 +15,9 @@ import ( // // Eg: // Order("id desc") +// Order("id", "desc") // Order("id desc,name asc"). -// Order("id", "desc",gdb.Raw("field(id, 3,1,2)")) +// Order(gdb.Raw("field(id, 3,1,2)")) func (m *Model) Order(orderBy ...interface{}) *Model { if len(orderBy) == 0 { return m @@ -28,15 +29,15 @@ func (m *Model) Order(orderBy ...interface{}) *Model { } for _, o := range orderBy { - switch o.(type) { - case Raw: - model.orderBy += gconv.String(o) - default: - model.orderBy += model.db.GetCore().QuoteString(gconv.String(o)) + if v, ok := o.(Raw); ok { + model.orderBy += gconv.String(v) + return model } } - return m + model.orderBy += model.db.GetCore().QuoteString(strings.Join(gconv.SliceStr(orderBy), " ")) + + return model } // OrderAsc sets the "ORDER BY xxx ASC" statement for the model. diff --git a/database/gdb/gdb_z_mysql_model_test.go b/database/gdb/gdb_z_mysql_model_test.go index 42e0d761a..c174a2637 100644 --- a/database/gdb/gdb_z_mysql_model_test.go +++ b/database/gdb/gdb_z_mysql_model_test.go @@ -441,10 +441,10 @@ func Test_Model_Clone(t *testing.T) { count, err := md.Count() t.AssertNil(err) - record, err := md.Order("id DESC").One() + record, err := md.Safe(true).Order("id DESC").One() t.AssertNil(err) - result, err := md.Order("id ASC").All() + result, err := md.Safe(true).Order("id ASC").All() t.AssertNil(err) t.Assert(count, 2)