mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-09-21 00:46:07 +02:00
713802622b
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@41703 467b73ca-7a2a-4603-9d3b-597d59a354a9
97 lines
2.0 KiB
PHP
97 lines
2.0 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Single field in the database.
|
|
* Every field from the database is represented as a sub-class of DBField. In addition to supporting
|
|
* the creation of the field in the database,
|
|
*/
|
|
abstract class DBField extends ViewableData {
|
|
protected $value;
|
|
protected $tableName;
|
|
protected $name;
|
|
|
|
function __construct($name) {
|
|
$this->name = $name;
|
|
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Create a DBField object that's not bound to any particular field.
|
|
* Useful for accessing the classes behaviour for other parts of your code.
|
|
*/
|
|
static function create($className, $value, $name = null) {
|
|
$dbField = Object::create($className, $name);
|
|
$dbField->setValue($value);
|
|
return $dbField;
|
|
}
|
|
|
|
function setVal($value) {
|
|
return $this->setValue($value);
|
|
}
|
|
function setValue($value) {
|
|
$this->value = $value;
|
|
}
|
|
function setTable($tableName) {
|
|
$this->tableName = $tableName;
|
|
}
|
|
function forTemplate() {
|
|
return $this->value;
|
|
}
|
|
|
|
function HTMLATT() {
|
|
return Convert::raw2htmlatt($this->value);
|
|
}
|
|
function ATT() {
|
|
return Convert::raw2att($this->value);
|
|
}
|
|
function RAW() {
|
|
return $this->value;
|
|
}
|
|
function JS() {
|
|
return Convert::raw2js($this->value);
|
|
}
|
|
function HTML(){
|
|
return Convert::raw2xml($this->value);
|
|
}
|
|
function XML(){
|
|
return Convert::raw2xml($this->value);
|
|
}
|
|
|
|
/**
|
|
* Returns the value to be set in the database to blank this field.
|
|
* Usually it's a choice between null, 0, and ''
|
|
*/
|
|
function nullValue() {
|
|
return "null";
|
|
}
|
|
|
|
/**
|
|
* Saves this field to the given data object.
|
|
*/
|
|
function saveInto($dataObject) {
|
|
$fieldName = $this->name;
|
|
if($fieldName) {
|
|
$dataObject->$fieldName = $this->value;
|
|
} else {
|
|
user_error("DBField::saveInto() Called on a nameless '" . get_class($this) . "' object", E_USER_ERROR);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Add the field to the underlying database.
|
|
*/
|
|
abstract function requireField();
|
|
|
|
function debug() {
|
|
return <<<DBG
|
|
<ul>
|
|
<li><b>Name:</b>{$this->name}</li>
|
|
<li><b>Table:</b>{$this->tableName}</li>
|
|
<li><b>Value:</b>{$this->value}</li>
|
|
</ul>
|
|
DBG;
|
|
}
|
|
}
|
|
|
|
?>
|