MCPcopy
hub / github.com/pocketbase/pocketbase / SetIfFieldExists

Method SetIfFieldExists

core/record_model.go:867–887  ·  view source on GitHub ↗

SetIfFieldExists sets the provided key-value data pair into the current Record model ONLY if key is existing Collection field name/modifier. This method does nothing if key is not a known Collection field name/modifier. On success returns the matched Field, otherwise - nil. To set any key-value,

(key string, value any)

Source from the content-addressed store, hash-verified

865//
866// To set any key-value, including custom/unknown fields, use the [Record.Set] method.
867func (m *Record) SetIfFieldExists(key string, value any) Field {
868 for _, field := range m.Collection().Fields {
869 ff, ok := field.(SetterFinder)
870 if ok {
871 setter := ff.FindSetter(key)
872 if setter != nil {
873 setter(m, value)
874 return field
875 }
876 }
877
878 // fallback to the default field PrepareValue method for direct match
879 if key == field.GetName() {
880 value, _ = field.PrepareValue(m, value)
881 m.SetRaw(key, value)
882 return field
883 }
884 }
885
886 return nil
887}
888
889// Set sets the provided key-value data pair into the current Record model.
890//

Callers 3

SetMethod · 0.95
ReplaceModifiersMethod · 0.80
LoadMethod · 0.80

Calls 5

CollectionMethod · 0.95
SetRawMethod · 0.95
FindSetterMethod · 0.65
GetNameMethod · 0.65
PrepareValueMethod · 0.65

Tested by

no test coverage detected