mirror of
https://github.com/astaxie/beego.git
synced 2024-11-26 03:11:30 +00:00
add RegisterModelWithSuffix #2140
This commit is contained in:
parent
c697b98006
commit
7d6c45d4c9
@ -22,8 +22,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// register models.
|
// register models.
|
||||||
// prefix means table name prefix.
|
// PrefixOrSuffix means table name prefix or suffix.
|
||||||
func registerModel(prefix string, model interface{}) {
|
// isPrefix whether the prefix is prefix or suffix
|
||||||
|
func registerModel(PrefixOrSuffix string, model interface{}, isPrefix bool) {
|
||||||
val := reflect.ValueOf(model)
|
val := reflect.ValueOf(model)
|
||||||
typ := reflect.Indirect(val).Type()
|
typ := reflect.Indirect(val).Type()
|
||||||
|
|
||||||
@ -39,8 +40,12 @@ func registerModel(prefix string, model interface{}) {
|
|||||||
|
|
||||||
table := getTableName(val)
|
table := getTableName(val)
|
||||||
|
|
||||||
if prefix != "" {
|
if PrefixOrSuffix != "" {
|
||||||
table = prefix + table
|
if isPrefix {
|
||||||
|
table = PrefixOrSuffix + table
|
||||||
|
} else {
|
||||||
|
table = table + PrefixOrSuffix
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// models's fullname is pkgpath + struct name
|
// models's fullname is pkgpath + struct name
|
||||||
name := getFullName(typ)
|
name := getFullName(typ)
|
||||||
@ -213,7 +218,6 @@ func bootStrap() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if fi.reverseFieldInfoTwo == nil {
|
if fi.reverseFieldInfoTwo == nil {
|
||||||
err = fmt.Errorf("can not find m2m field for m2m model `%s`, ensure your m2m model defined correct",
|
err = fmt.Errorf("can not find m2m field for m2m model `%s`, ensure your m2m model defined correct",
|
||||||
fi.relThroughModelInfo.fullName)
|
fi.relThroughModelInfo.fullName)
|
||||||
@ -297,17 +301,31 @@ end:
|
|||||||
|
|
||||||
// RegisterModel register models
|
// RegisterModel register models
|
||||||
func RegisterModel(models ...interface{}) {
|
func RegisterModel(models ...interface{}) {
|
||||||
|
if modelCache.done {
|
||||||
|
panic(fmt.Errorf("RegisterModel must be run before BootStrap"))
|
||||||
|
}
|
||||||
RegisterModelWithPrefix("", models...)
|
RegisterModelWithPrefix("", models...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterModelWithPrefix register models with a prefix
|
// RegisterModelWithPrefix register models with a prefix
|
||||||
func RegisterModelWithPrefix(prefix string, models ...interface{}) {
|
func RegisterModelWithPrefix(prefix string, models ...interface{}) {
|
||||||
if modelCache.done {
|
if modelCache.done {
|
||||||
panic(fmt.Errorf("RegisterModel must be run before BootStrap"))
|
panic(fmt.Errorf("RegisterModelWithPrefix must be run before BootStrap"))
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, model := range models {
|
for _, model := range models {
|
||||||
registerModel(prefix, model)
|
registerModel(prefix, model, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// RegisterModelWithSuffix register models with a suffix
|
||||||
|
func RegisterModelWithSuffix(suffix string, models ...interface{}) {
|
||||||
|
if modelCache.done {
|
||||||
|
panic(fmt.Errorf("RegisterModelWithSuffix must be run before BootStrap"))
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, model := range models {
|
||||||
|
registerModel(suffix, model, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user