Jump to:

3447 Posts in 1061 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Boolean Property with default 'true'

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

Page: 1
Go to End
Author Topic: 1261 Views
  • rob.s
    Avatar
    Community Member
    78 Posts

    Boolean Property with default 'true' Link to this post

    Hi,

    is it possible to create a boolean db property with '1' as default.
    The static property array $defaults does not the job.
    When the field is created (dev/build) the SQL Default is always '0'´.

    I need the '1' as default.

    Is this possible?

    Robert

  • swaiba
    Avatar
    Forum Moderator
    1788 Posts

    Re: Boolean Property with default 'true' Link to this post

    I think this is what you are after...

       function populateDefaults(){
          $this->MyBooleanField = 1;
       }

  • rob.s
    Avatar
    Community Member
    78 Posts

    Re: Boolean Property with default 'true' Link to this post

    Hi swaiba,

    thanks for your reply.
    But this is not what im after.

    Boolean properties are always "Default 0"

    Field TableName.MyBoolean: created as tinyint(1) unsigned not null default 0


    I'm searching for a solution that populates the SQL Defaults to 1 instead of 0

    The only approach i am thinking about is to define the property as Enum("0,1","1") and cast it back as boolean.
    But this is ugly .....

  • swaiba
    Avatar
    Forum Moderator
    1788 Posts

    Re: Boolean Property with default 'true' Link to this post

    it looks like it should...

    sapphire\core\model\MySQLDatabase.php

    /**
    * Return a boolean type-formatted string
    *
    * @param array $values Contains a tokenised list of info about this data type
    * @return string
    */
    public function boolean($values){
       //For reference, this is what typically gets passed to this function:
       //$parts=Array('datatype'=>'tinyint', 'precision'=>1, 'sign'=>'unsigned', 'null'=>'not null', 'default'=>$this->default);
       //DB::requireField($this->tableName, $this->name, "tinyint(1) unsigned not null default '{$this->defaultVal}'");   
       return 'tinyint(1) unsigned not null default ' . (int)$values['default'];
    }

    you could always... hack the core!

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Boolean Property with default 'true' Link to this post

    You could put it in the onBeforeWrite() for your object

    public function onBeforeWrite() {
    $this->Field = 1;

    parent::onBeforeWrite();
    }

    All objects should use write() to save to the database so that will act as a default. If you only wish to set it on default use the fact that the object isn't in the database at first

    public function onBeforeWrite() {
    if(!$this->isInDB()) {
    $this->Field = 1;
    }
    parent::onBeforeWrite();
    }

  • rob.s
    Avatar
    Community Member
    78 Posts

    Re: Boolean Property with default 'true' Link to this post

    Hi Willr,

    thanks for your ideas.
    But this will not work, because i have to use it on a Model where a lot of records already exist.
    This means, that when new new boolean field is created - all records will have the initial value "0".

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Boolean Property with default 'true' Link to this post

    Simply write a build task (or do a migration as part of requireDefaultRecords) which sets your default value on existing data. You should be able to set the default value in the $db structure but doesn't seem to work for me either.

  • Oilee80
    Avatar
    Community Member
    1 Post

    Re: Boolean Property with default 'true' Link to this post

    I doubt this is still needed but I have found a way

    in the $db define the type as

    public static $db => array(
    'Field' => 'Boolean(1)'
    );

    Boolean seems to pass whatever is in the brackets as the Default value ???

    Seems to completly ignore the

    $defaults

    property

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