As designed Associative arrays as a default values in Serialized DataWriter fields.

Minterwute

New member
I've run into an issue developing an addon for XF 1.4.3.

Setting a non-empty associative array as the default value for a field of type TYPE_SERIALIZED (or TYPE_JSON) in a DataWriter will result in an error caused by an attempted read on index 0.

Steps to reproduce the issue:
  1. Add a field to a new or existing DataWriter, with TYPE_SERIALIZED.
  2. Assign the default value of the field to a non-empty associative array.
ie.
Code:
// Given fields is the array return by _getFields()

$fields['my_field'] = array(
    'type'    => self::TYPE_SERIALIZED,
    'default' => array(
        'foo' => 'bar'
     )
);

Attempting a write operation that involves this field will produce the following error:
Undefined offset: 0 - library\XenForo\DataWriter.php:1681
 
This is actually as designed.

See XenForo_DataWriter_User for a typical usage.

Where the default value is an array, it is actually designed to pull the default value from another table managed by the DataWriter, e.g. the default user_id field value for the xf_user_profile table will come from the value set for user_id in the xf_user table.

If you actually wanted to specify an actual default value there, you would want to serialize the default array:

PHP:
// Given fields is the array return by _getFields()

$fields['my_field'] = array(
    'type'    => self::TYPE_SERIALIZED,
    'default' => serialize(array(
        'foo' => 'bar'
     ))
);
 
Top Bottom