Version: current

     1 <?php
     2 /**
     3  * An abstract base class for the string field types (i.e. Varchar and Text)
     4  * @package sapphire
     5  * @subpackage model
     6  * @author Pete Bacon Darwin
     7  *
     8  */
     9 abstract class StringField extends DBField {
    10     protected $nullifyEmpty = true;
    11
    12     /**
    13      * Construct a string type field with a set of optional parameters
    14      * @param $name string The name of the field
    15      * @param $options array An array of options e.g. array('nullifyEmpty'=>false).  See {@link StringField::setOptions()} for information on the available options
    16      */
    17     function __construct($name = null, $options = array()) {
    18         // Workaround: The singleton pattern calls this constructor with true/1 as the second parameter, so we must ignore it
    19         if(is_array($options)){
    20             $this->setOptions($options);
    21         }
    22         parent::__construct($name);
    23     }
    24
    25     /**
    26      * Update the optional parameters for this field.
    27      * @param $options array of options
    28      * The options allowed are:
    29      *   <ul><li>"nullifyEmpty"
    30      *       This is a boolean flag.
    31      *       True (the default) means that empty strings are automatically converted to nulls to be stored in the database.
    32      *       Set it to false to ensure that nulls and empty strings are kept intact in the database.
    33      *   </li></ul>
    34      * @return unknown_type
    35      */
    36     function setOptions(array $options = array()) {
    37         if(array_key_exists("nullifyEmpty", $options)) {
    38             $this->nullifyEmpty = $options["nullifyEmpty"] ? true : false;
    39         }
    40     }
    41
    42     /**
    43      * Set whether this field stores empty strings rather than converting them to null
    44      * @param $value boolean True if empty strings are to be converted to null
    45      */
    46     function setNullifyEmpty($value) {
    47         $this->nullifyEmpty == $value ? true : false;
    48     }
    49     /**
    50      * Get whether this field stores empty strings rather than converting them to null
    51      * @return bool True if empty strings are to be converted to null
    52      */
    53     function getNullifyEmpty() {
    54         return $this->nullifyEmpty;
    55     }
    56
    57     /**
    58      * (non-PHPdoc)
    59      * @see core/model/fieldtypes/DBField#hasValue()
    60      */
    61     function hasValue() {
    62         return ($this->value || $this->value == '0') || ( !$this->nullifyEmpty && $this->value === '');
    63     }
    64
    65     /**
    66      * (non-PHPdoc)
    67      * @see core/model/fieldtypes/DBField#prepValueForDB($value)
    68      */
    69     function prepValueForDB($value) {
    70         if ( !$this->nullifyEmpty && $value === '' ) {
    71             return "'" . Convert::raw2sql($value) . "'";
    72         } else {
    73             return parent::prepValueForDB($value);
    74         }
    75     }
    76
    77
    78     /**
    79      * Return another DBField object with this value in lowercase.
    80      */
    81     function Lower() {
    82         return DBField::create(get_class($this), strtolower($this->value), $this->name);
    83     }
    84
    85     /**
    86      * Return another DBField object with this value in uppercase.
    87      */
    88     function Upper() {
    89         return DBField::create(get_class($this), strtoupper($this->value), $this->name);
    90     }
    91
    92 }
    93

[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. -