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

refael

Well-known member
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
 
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:
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...
 
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.
 
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.
 
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, :)
 
Back
Top Bottom