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.

Archive /

Our old forums are still available as a read-only archive.

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

Set default value to data-fields


Go to End


6 Posts   19203 Views

Avatar
cobiashi

Community Member, 5 Posts

3 June 2008 at 7:09pm

I've tried static $defaults = array ('Title' => 'DefaultValue');

but it doesn't seem to work. I also attempted to use onBeforeWrite(), but this is upon saving, not upon creating the actual new Page.

is there some simple way to do this?

Thanks!

Avatar
Willr

Forum Moderator, 5523 Posts

3 June 2008 at 7:53pm

$defaults is the correct way to do it. Does the space between array ( ) make a difference? rather then array()??

Avatar
cobiashi

Community Member, 5 Posts

20 June 2008 at 5:49am

no, that doesn't work. i've drop the table and tried it over and over again.

here's the code:

static $db = array(
		'Title' => 'Varchar(150)',
		'Teaser' => 'Text',
		'MemberPrice' => 'Currency',
		'GenPrice' => 'Currency',
		'TicketLinkText' => 'Varchar(60)',
		'TicketLinkURL' => 'Varchar(255)'
	);
	
	static $defaults = array('TicketLinkText' => 'Buy Tickets');

and when creating this database i get:

# Table SubSeries: created
# Field SubSeries.ClassName: created as enum('SubSeries') character set utf8 collate utf8_general_ci default 'SubSeries'
# Field SubSeries.Created: created as datetime
# Field SubSeries.LastEdited: created as datetime
# Field SubSeries.Title: created as varchar(150) character set utf8 collate utf8_general_ci
# Field SubSeries.Teaser: created as mediumtext character set utf8 collate utf8_general_ci
# Field SubSeries.MemberPrice: created as decimal(9,2)
# Field SubSeries.GenPrice: created as decimal(9,2)
# Field SubSeries.TicketLinkText: created as varchar(60) character set utf8 collate utf8_general_ci
# Field SubSeries.TicketLinkURL: created as varchar(255) character set utf8 collate utf8_general_ci
# Field SubSeries.ImageID: created as int(11) not null default '0'
# Field SubSeries.LocationID: created as int(11) not null default '0'
# Index SubSeries.ImageID: created as (ImageID)
# Index SubSeries.LocationID: created as (LocationID)
# Index SubSeries.ClassName: created as (ClassName)

but no default is set.

any help would be great. thanks!

Avatar
(deleted)

Community Member, 473 Posts

20 June 2008 at 7:23am

The default doesn't get set in the database. Does creating a new page have the default set?

Avatar
cobiashi

Community Member, 5 Posts

1 July 2008 at 5:41am

no it doesn't fill out any fields.

the issue could be that this is a dataobject and not a page. Do you think thats the problem, and if so, is there a way to set defaults for data objects?

Avatar
Sam

Administrator, 690 Posts

7 July 2008 at 2:55pm

$defaults doesn't affect the MySQL database default; it sets default values when you create new DataObjects. It works for DataObject as well as SiteTree.

Make sure that when you're creating the new DataObject, you're not passing any arguments to the constructor. The array argument for setting DataObject's fields should only be used when creating existing database records from the database. Because it's designed for that, it bypasses the populateDefaults() method that sets defaults.