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

Discussion in 'Resolved Bug Reports' started by rellect, Feb 19, 2016.

  1. rellect

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

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

    Snog Well-Known Member

    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: Feb 19, 2016
  3. Chris D

    Chris D XenForo Developer Staff Member

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

    Mike XenForo Developer Staff Member

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

    rellect Well-Known Member

    Well not exactly, it also accepts true and false
    UPDATE `xf_user_fieldSET `example` = false
    But again, not a big deal..
  6. Snog

    Snog Well-Known Member

    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..
    $value false;
    UPDATE `xf_user_fieldSET `example` = $value
    Because $value is set to '' by PHP. Not to 0.
  7. rellect

    rellect Well-Known Member

    Obviously not, but ideally the data writer should know how to handle this before passing it to the query
  8. Lawrence

    Lawrence Well-Known Member

    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, :)

Share This Page