silverstripe-framework/model/FieldType/DBVarchar.php
Sam Minnee aeccb8b8e0 API: Move DBField subclasses into SilverStripe\Model\FieldType namespace
API: Deprecate SS_Datetime.

The DBField subclasses are have all been renamed to start with “DB” and
be in the SilverStripe\Model\FieldType namespace. To keep DataObject
definitions concise, the original short variations of their names are
preserved as service definitions. Most of the field generation code
doesn’t need to change, but where field classes are referenced directly,
changes will be needed.

SS_Datetime, which is commonly referenced outside the model system
itself, has been preserved as a subclass of DBDatetime. This has been
marked as deprecated and can be removed in SilverStripe 5.

A few places that referred to $db and $casting values weren’t using
the Injector to instantiate the relevant classes. This meant that the
remapping we have created as part of moving classes into a namespace
didn’t work.
2016-03-22 18:09:30 +13:00

119 lines
2.8 KiB
PHP

<?php
namespace SilverStripe\Model\FieldType;
use DB;
use NullableField;
use TextField;
use Config;
/**
* 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 DBVarchar extends DBString {
private 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
*/
public function __construct($name = null, $size = 50, $options = array()) {
$this->size = $size ? $size : 50;
parent::__construct($name, $options);
}
/**
* Allow the ability to access the size of the field programatically. This
* can be useful if you want to have text fields with a length limit that
* is dictated by the DB field.
*
* TextField::create('Title')->setMaxLength(singleton('SiteTree')->dbObject('Title')->getSize())
*
* @return int The size of the field
*/
public function getSize() {
return $this->size;
}
/**
* (non-PHPdoc)
* @see DBField::requireField()
*/
public function requireField() {
$charset = Config::inst()->get('MySQLDatabase', 'charset');
$collation = Config::inst()->get('MySQLDatabase', 'collation');
$parts = array(
'datatype'=>'varchar',
'precision'=>$this->size,
'character set'=> $charset,
'collate'=> $collation,
'arrayValue'=>$this->arrayValue
);
$values = array(
'type' => 'varchar',
'parts' => $parts
);
DB::require_field($this->tableName, $this->name, $values);
}
/**
* Return the first letter of the string followed by a .
*/
public function Initial() {
if($this->exists()) return $this->value[0] . '.';
}
/**
* Ensure that the given value is an absolute URL.
*/
public 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
*/
public 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);
}
}
}