3063 Posts in 864 Topics by 646 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 295 Views |
-
Boolean Property with default 'true'

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
-
Re: Boolean Property with default 'true'

11 January 2013 at 11:30am
I think this is what you are after...
function populateDefaults(){
$this->MyBooleanField = 1;
} -
Re: Boolean Property with default 'true'

11 January 2013 at 7:57pm Last edited: 11 January 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 0The 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 ..... -
Re: Boolean Property with default 'true'

11 January 2013 at 10:30pm Last edited: 11 January 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!
-
Re: Boolean Property with default 'true'

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();
} -
Re: Boolean Property with default 'true'

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". -
Re: Boolean Property with default 'true'

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.
| 295 Views | ||
|
Page:
1
|
Go to Top |



