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!
This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.
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.
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!
$defaults is the correct way to do it. Does the space between array ( ) make a difference? rather then array()??
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!
The default doesn't get set in the database. Does creating a new page have the default set?
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?
$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.