• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

As designed Cannot use booleans (true/false) as default value in datawriter

rellect

Well-known member
#1
Though it's very small matter, thought to mention.
I noticed that when default value is true/false for a boolean field it won't work as expected. It only works with 0/1 as value.
Since the field is boolean, I would expect that true/false would be a valid value.

PHP:
protected function _getFields()
{
    $fields = parent::_getFields();
    $fields['xf_user_field']['example'] = array(
        'type' => self::TYPE_BOOLEAN,
        'default' => false
    );
    return $fields;
}
The error I receive is
PHP:
Mysqli statement execute error : Incorrect integer value: '' for column 'example' at row 1
 

Snog

Well-known member
#2
MySql boolean fields are actually tinyint(1) fields. And as such expect an integer value. ;)

PHP itself returns '' for a boolean FALSE value. So you are setting a '' into an integer field.
 
Last edited:

Chris D

XenForo developer
Staff member
#3
This is generally what I would expect to happen.

The same sort of thing applies to serialized fields. These require a default value of a string, too, defaults aren't automatically cast before being inserted into the database. Arguably, we could do that, but that would likely be a future change...
 

Mike

XenForo developer
Staff member
#4
I'm going to call this as designed. As noted, the default is expected to be the value in the DB format, rather than a PHP value. There appear to be a couple places where we've made this mistake, but 99.9% use 1/0 as expected.
 

Snog

Well-known member
#6
Well not exactly, it also accepts true and false
PHP:
UPDATE `xf_user_field` SET `example` = false
But again, not a big deal..
In a query, MySql interprets True/False as 1/0. ;)

That's not the same as setting a value using a PHP True/False.

Something similar to this would throw an error..
PHP:
$value = false;
UPDATE `xf_user_field` SET `example` = $value
Because $value is set to '' by PHP. Not to 0.
 

Lawrence

Well-known member
#8
Personally, I think it should be up to the developer to ensure the data being sent to the data writers is of the acquired type. However, having a data writer handle true false values would be consistent with how they handle other types, :)