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

SS3 Defaults on a dataobject


Go to End
Reply


3 Posts   577 Views

Avatar
merrick_sd

Community Member, 98 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, 98 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, 187 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)