Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

SS3 Defaults on a dataobject


Reply


3 Posts   404 Views

Avatar
merrick_sd

Community Member, 97 Posts

4 December 2012 at 12:49am

Version Silvertsripe 3

Question: can you have defaults on a DataObject?

class EstateItem extends DataObject
{

static $db = array(
   'Active' => 'Boolean',);

static $defaults = array(   
      'Active' => true
   );

}

this doesn't seem to work or 'Active' => 1

Avatar
merrick_sd

Community Member, 97 Posts

4 December 2012 at 1:45am

   public function populateDefaults() {
    parent::populateDefaults();
       $this->Active = 1;
       $this->URLSegment = 'new-EstateItem';
   }

function getCMSFields()
   {
      $fields = parent::getCMSFields();

...
EstateItem::populateDefaults();

}

EstateItem::populateDefaults(); seems to work ..but i only want that to happen for new records only.

Avatar
kinglozzer

Community Member, 175 Posts

4 December 2012 at 5:50am

For each of the fields you're adding in your getCMSFields() function, can you not do this?

$field->setValue('Some default value');

You can also check if it's a new record with:

if ($this->ID)