silverstripe-framework/model/fieldtypes/Varchar.php
Marcus Nyeholt 82495f5a7e BUGFIX Versioned's constructor doesn't provide suitable defaults. Previously a bug/feature in singleton, where it would pass null,true as params to strong_create, which would then get passed through as params to Versioned's constructor, meant that the code still executed fine (as was set to something that wasn't an array, so the null and true were instead taken as args). The fact that the usage of singleton(Versioned) never really used the classes code, purely for value lookup, meant that this never propagated errors. I've now switched singleton() to use the injector for retrieving values, which means these dud values are no longer passed through
CHANGE Given that Config::inst is an implementation of the singleton pattern itself, I've removed the extra call to singleton(). A side effect of this is that it gets around a possibly nasty circular reference with the dependency injector (which relies on the config object); in future, this dependency structure should really be structured from the DI directly.

MINOR Change singleton and strong_create to use dependency injector

BUGFIX: Provide default constructor values for classes (fixes issues when used in 'singleton' scenario during dev/build in particular)

MINOR Clear out injector state when resetting db schema during tests (a follow on from changing singleton() calls to use the injector underneath)
2012-05-23 21:10:04 +10:00

94 lines
2.2 KiB
PHP

<?php
/**
* Class Varchar represents a variable-length string of up to 255 characters, designed to store raw text
*
* @see HTMLText
* @see HTMLVarchar
* @see Text
*
* @package framework
* @subpackage model
*/
class Varchar extends StringField {
static $casting = array(
"Initial" => "Text",
"URL" => "Text",
);
protected $size;
/**
* Construct a new short text field
*
* @param $name string The name of the field
* @param $size int The maximum size of the field, in terms of characters
* @param $options array Optional parameters, e.g. array("nullifyEmpty"=>false). See {@link StringField::setOptions()} for information on the available options
* @return unknown_type
*/
function __construct($name = null, $size = 50, $options = array()) {
$this->size = $size ? $size : 50;
parent::__construct($name, $options);
}
/**
* (non-PHPdoc)
* @see DBField::requireField()
*/
function requireField() {
$parts = array(
'datatype'=>'varchar',
'precision'=>$this->size,
'character set'=>'utf8',
'collate'=>'utf8_general_ci',
'arrayValue'=>$this->arrayValue
);
$values = array(
'type' => 'varchar',
'parts' => $parts
);
DB::requireField($this->tableName, $this->name, $values);
}
/**
* Return the first letter of the string followed by a .
*/
function Initial() {
if($this->exists()) return $this->value[0] . '.';
}
/**
* Ensure that the given value is an absolute URL.
*/
function URL() {
if(preg_match('#^[a-zA-Z]+://#', $this->value)) return $this->value;
else return "http://" . $this->value;
}
/**
* Return the value of the field in rich text format
* @return string
*/
function RTF() {
return str_replace("\n", '\par ', $this->value);
}
/**
* (non-PHPdoc)
* @see DBField::scaffoldFormField()
*/
public function scaffoldFormField($title = null, $params = null) {
if(!$this->nullifyEmpty) {
// Allow the user to select if it's null instead of automatically assuming empty string is
return new NullableField(new TextField($this->name, $title));
} else {
// Automatically determine null (empty string)
return parent::scaffoldFormField($title);
}
}
}