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.

Archive

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

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

Set default value to data-fields


Reply

6 Posts   11207 Views

Avatar
cobiashi

3 June 2008 at 7:09pm Community Member, 5 Posts

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

3 June 2008 at 7:53pm Forum Moderator, 5511 Posts

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

Avatar
cobiashi

20 June 2008 at 5:49am Community Member, 5 Posts

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
simon_w

20 June 2008 at 7:23am Forum Moderator, 474 Posts

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

Avatar
cobiashi

1 July 2008 at 5:41am Community Member, 5 Posts

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

7 July 2008 at 2:55pm Administrator, 685 Posts

$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.