Version: current

# Class DBField

Single field in the database. Every field from the database is represented as a sub-class of DBField.

## Multi-value DBField objects

Sometimes you will want to make DBField classes that don't have a 1-1 match to database fields. To do this, there are a number of fields for you to overload. - Overload `DBField::writeToManipulation()` to add the appropriate references to the INSERT or UPDATE command - Overload `DBField::addToQuery()` to add the appropriate items to a SELECT query's field list - Add appropriate accessor methods

  Object
  ViewableData implements IteratorAggregate
  DBField

### Direct known subclasses

`Boolean`, `Date`, `Year`, `Decimal`, `Double`, `Enum`, `Float`, `Int`, `Money`, `StringField`, `Time`

### Indirect known subclasses

`CatalogPrice`, `Currency`, `SS_Datetime`, `Text`, `Varchar`, `DBLocale`, `ForeignKey`, `HTMLText`, `HTMLVarchar`, `MultiEnum`, `Percentage`, `PrimaryKey`, `ShopCountry`

Abstract
Package: sapphire\model
Located at sapphire/core/model/fieldtypes/DBField.php

## Methods summary

`public` | # `__construct( mixed $name = null )` #### Overrides `Object::__construct`
`public static `Object`` | # `create( string $className, mixed $arguments,…, mixed $name = null, mixed $object = null )` Create a DBField object that's not bound to any particular field. Useful for accessing the classes behaviour for other parts of your code. Create a DBField object that's not bound to any particular field. Useful for accessing the classes behaviour for other parts of your code. #### Parameters $className string $class the class name $arguments,… mixed $arguments,... arguments to pass to the constructor $name $object #### Returns ``Object`` Object #### Overrides `Object::create`
`public` | # `setName( mixed $name )` Set the name of this field. The name should never be altered, but it if was never given a name in the first place you can set a name. If you try an alter the name a warning will be thrown. Set the name of this field. The name should never be altered, but it if was never given a name in the first place you can set a name. If you try an alter the name a warning will be thrown.
`public string` | # `getName( )` Returns the name of this field. Returns the name of this field. #### Returns `string` string
`public mixed` | # `getValue( )` Returns the value of this field. Returns the value of this field. #### Returns `mixed` mixed
`public` | # `setValue( mixed $value, array $record = null )` Set the value on the field. Optionally takes the whole record as an argument, to pick other values. Set the value on the field. Optionally takes the whole record as an argument, to pick other values. #### Parameters $value mixed $value $record array $record
`public boolean` | # `hasValue( )` Determines if the field has a value which is not considered to be 'null' in a database context. Determines if the field has a value which is not considered to be 'null' in a database context. #### Returns `boolean` boolean #### Overrides `ViewableData::hasValue`
`public boolean` | # `exists( )` #### Returns `boolean` bool #### Overrides `Object::exists`
`public string` | # `prepValueForDB( mixed $value )` Return an encoding of the given value suitable for inclusion in a SQL statement. If necessary, this should include quotes. Return an encoding of the given value suitable for inclusion in a SQL statement. If necessary, this should include quotes. #### Parameters $value $value mixed The value to check #### Returns `string` The encoded value
`public` | # `writeToManipulation( array & $manipulation )` Prepare the current field for usage in a database-manipulation (works on a manipulation reference). Prepare the current field for usage in a database-manipulation (works on a manipulation reference). Make value safe for insertion into a SQL SET statement by applying addslashes() - can also be used to apply special SQL-commands to the raw value (e.g. for GIS functionality). `DBField::prepValueForDB()` #### Parameters $manipulation array $manipulation
`public` | # `addToQuery( `SS_Query` & $query )` Add custom query parameters for this field, mostly SELECT statements for multi-value fields. Add custom query parameters for this field, mostly SELECT statements for multi-value fields. By default, the ORM layer does a SELECT <tablename>.* which gets you the default representations of all columns. #### Parameters $query SS_Query $query
`public` | # `setTable( mixed $tableName )`
`public string` | # `forTemplate( )` #### Returns `string` string
`public` | # `HTMLATT( )`
`public` | # `URLATT( )`
`public` | # `RAWURLATT( )`
`public` | # `ATT( )`
`public` | # `RAW( )`
`public` | # `JS( )`
`public` | # `HTML( )`
`public` | # `XML( )`
`public string` | # `LowerCase( )` Converts the current value for this Enum DBField to lowercase. Converts the current value for this Enum DBField to lowercase. #### Returns `string` string
`public string` | # `UpperCase( )` Converts the current value for this Enum DBField to uppercase. Converts the current value for this Enum DBField to uppercase. #### Returns `string` string
`public` | # `nullValue( )` Returns the value to be set in the database to blank this field. Usually it's a choice between null, 0, and '' Returns the value to be set in the database to blank this field. Usually it's a choice between null, 0, and ''
`public` | # `saveInto( mixed $dataObject )` Saves this field to the given data object. Saves this field to the given data object.
`public `FormField`` | # `scaffoldFormField( string $title = null )` Returns a FormField instance used as a default for form scaffolding. Returns a FormField instance used as a default for form scaffolding. Used by `SearchContext`, `ModelAdmin`, `DataObject::scaffoldFormFields()` #### Parameters $title string $title Optional. Localized title of the generated instance #### Returns ``FormField`` FormField #### Used by ``FormScaffolder``
`public `FormField`` | # `scaffoldSearchField( string $title = null )` Returns a FormField instance used as a default for searchform scaffolding. Returns a FormField instance used as a default for searchform scaffolding. Used by `SearchContext`, `ModelAdmin`, `DataObject::scaffoldFormFields()`. #### Parameters $title string $title Optional. Localized title of the generated instance #### Returns ``FormField`` FormField
`public `SearchFilter`` | # `defaultSearchFilter( mixed $name = false )` #### Returns ``SearchFilter`` SearchFilter
`abstract public` | # `requireField( )` Add the field to the underlying database. Add the field to the underlying database.
`public` | # `debug( )`

### Methods inherited from ViewableData

`ATT_val()`, `BaseHref()`, `CSSClasses()`, `ColumnBreak()`, `ColumnCalc()`, `ColumnNumber()`, `ColumnPad()`, `ColumnPos()`, `CurrentMember()`, `CurrentPage()`, `Debug()`, `Even()`, `EvenOdd()`, `First()`, `FirstLast()`, `HasPerm()`, `IsAjax()`, `JS_val()`, `Last()`, `Me()`, `Middle()`, `MiddleString()`, `Modulus()`, `MultipleOf()`, `Odd()`, `Pos()`, `RAW_val()`, `SQL_val()`, `ThemeDir()`, `ThemeName()`, `Top()`, `TotalItems()`, `XML_val()`, `__get()`, `__isset()`, `__set()`, `buildCastingCache()`, `cachedCall()`, `castingClass()`, `castingHelper()`, `castingHelperPair()`, `castingObjectCreator()`, `castingObjectCreatorPair()`, `customise()`, `defineMethods()`, `escapeTypeForField()`, `getField()`, `getIterator()`, `getSecurityID()`, `getXMLValues()`, `hasField()`, `i18nLocale()`, `iteratorProperties()`, `obj()`, `renderWith()`, `setCustomisedObj()`, `setField()`

### Methods inherited from Object

`__call()`, `__toString()`, `__wakeup()`, `addMethodsFrom()`, `addStaticVars()`, `addWrapperMethod()`, `add_extension()`, `add_static_var()`, `allMethodNames()`, `cacheToFile()`, `cacheToFileWithArgs()`, `clearCache()`, `combined_static()`, `createMethod()`, `create_from_string()`, `extInstance()`, `extend()`, `getCustomClass()`, `getExtensionInstance()`, `getExtensionInstances()`, `get_extensions()`, `get_static()`, `hasExtension()`, `hasMethod()`, `has_extension()`, `invokeWithExtensions()`, `is_a()`, `loadCache()`, `parentClass()`, `parse_class_spec()`, `remove_extension()`, `sanitiseCachename()`, `saveCache()`, `set_stat()`, `set_static()`, `set_uninherited()`, `stat()`, `strong_create()`, `uninherited()`, `uninherited_static()`, `useCustomClass()`

### Magic methods summary

## Properties summary

`protected mixed` | $value | #
`protected mixed` | $tableName | #
`protected mixed` | $name | #
`protected mixed` | $arrayValue | #
`public static string` | $escape_type | # The escape type for this field when inserted into a template - either "xml" or "raw". The escape type for this field when inserted into a template - either "xml" or "raw".
`public static string` | $default_search_filter_class | # Subclass of `SearchFilter` for usage in `DBField::defaultSearchFilter()`. Subclass of `SearchFilter` for usage in `DBField::defaultSearchFilter()`.
`protected mixed` | $defaultVal | # mixed Default-value in the database. Might be overridden on DataObject-level, but still useful for setting defaults on already existing records after a db-build. mixed Default-value in the database. Might be overridden on DataObject-level, but still useful for setting defaults on already existing records after a db-build.

### Properties inherited from ViewableData

`$casting`, `$customisedObject`, `$default_cast`, `$failover`, `$iteratorPos`, `$iteratorTotalItems`

### Properties inherited from Object

`$class`, `$extension_instances`, `$extensions`

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -