mirror of
https://gitee.com/johng/gf.git
synced 2024-11-29 18:57:44 +08:00
add table field method
This commit is contained in:
parent
78536de1b5
commit
534cd3be1c
@ -127,3 +127,24 @@ func (m *Model) FieldsExStr(fields string, prefix ...string) string {
|
||||
newFields = m.db.QuoteString(newFields)
|
||||
return newFields
|
||||
}
|
||||
|
||||
// HasField determine whether the field exists in the table.
|
||||
func (m *Model) HasField(field string) bool {
|
||||
tableFields, err := m.db.TableFields(m.tables)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if len(tableFields) == 0 {
|
||||
panic(fmt.Sprintf(`empty table fields for table "%s"`, m.tables))
|
||||
}
|
||||
fieldsArray := make([]string, len(tableFields))
|
||||
for k, v := range tableFields {
|
||||
fieldsArray[v.Index] = k
|
||||
}
|
||||
for _, f := range fieldsArray {
|
||||
if f == field {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
Loading…
Reference in New Issue
Block a user