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   335 Views

Avatar
merrick_sd

4 December 2012 at 12:49am Community Member, 95 Posts

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

4 December 2012 at 1:45am Community Member, 95 Posts

   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

4 December 2012 at 5:50am Community Member, 146 Posts

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)