Jump to:

23358 Posts in 18005 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » SS3 Defaults on a dataobject

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

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

Page: 1
Go to End
Author Topic: 311 Views
  • merrick_sd
    Avatar
    Community Member
    94 Posts

    SS3 Defaults on a dataobject Link to this post

    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

  • merrick_sd
    Avatar
    Community Member
    94 Posts

    Re: SS3 Defaults on a dataobject Link to this post

       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.

  • kinglozzer
    Avatar
    Community Member
    138 Posts

    Re: SS3 Defaults on a dataobject Link to this post

    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)

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