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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

SS3 Defaults on a dataobject


Go to End


3 Posts   794 Views

Avatar
merrick_sd

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