Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Set default value to data-fields

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

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

Page: 1
Go to End
Author Topic: 11138 Views
  • cobiashi
    Avatar
    Community Member
    5 Posts

    Set default value to data-fields Link to this post

    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!

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Set default value to data-fields Link to this post

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

  • cobiashi
    Avatar
    Community Member
    5 Posts

    Re: Set default value to data-fields Link to this post

    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!

  • simon_w
    Avatar
    Forum Moderator
    473 Posts

    Re: Set default value to data-fields Link to this post

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

  • cobiashi
    Avatar
    Community Member
    5 Posts

    Re: Set default value to data-fields Link to this post

    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?

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: Set default value to data-fields Link to this post

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

    11138 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.