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.

We've moved the forum!

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.

Data Model Questions /

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

Boolean Property with default 'true'


Go to End


8 Posts   9191 Views

Avatar
rob.s

Community Member, 78 Posts

8 January 2013 at 10:42pm

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

Avatar
swaiba

Forum Moderator, 1899 Posts

11 January 2013 at 11:30am

I think this is what you are after...

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

Avatar
rob.s

Community Member, 78 Posts

11 January 2013 at 7:57pm

Edited: 11/01/2013 7:58pm

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

Avatar
swaiba

Forum Moderator, 1899 Posts

11 January 2013 at 10:30pm

Edited: 11/01/2013 10:31pm

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!

Avatar
Willr

Forum Moderator, 5523 Posts

13 January 2013 at 8:50pm

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();
}

Avatar
rob.s

Community Member, 78 Posts

13 January 2013 at 9:33pm

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

Avatar
Willr

Forum Moderator, 5523 Posts

13 January 2013 at 9:35pm

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.

Avatar
Oilee80

Community Member, 1 Post

21 May 2014 at 12:54am

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