Using NULL in UINT field with DataWriter

Jake B.

Well-known member
We need to be able to set a field to null for some certain circumstances. However, when I try to pass null to the DataWriter it defaults it to a Zero, which has a separate meaning. Right now we have this:

NULL = Use global setting for this field
0 = Disable this setting for this location only
1+ = Number of seconds for advertisement to show up.

Relevant code:

data being passed to DataWriter:

Code:
$rotateSpeedStr = $this->_input->filterSingle('rotate_speed', XenForo_Input::STRING);
$rotateSpeed = $this->_input->filterSingle('rotate_speed', XenForo_Input::UINT);

$writerData = $this->_input->filter(array(
    'title'            => XenForo_Input::STRING,
    'hook_name'        => XenForo_Input::STRING,
    'active'        => XenForo_Input::UINT,
    'global'        => XenForo_Input::UINT,
));

$writerData['rotate_speed'] = $rotateSpeed;
if (empty($rotateSpeedStr))
{
    $writerData['rotate_speed'] = 'null';
}

DataWriter field definitions:
Code:
protected function _getFields()
{
    return array(
        'admonetize_advertisement_spot'    => array(
            'spot_id'        => array('type' => self::TYPE_UINT, 'autoIncrement' => true),
            'title'            => array('type' => self::TYPE_STRING, 'maxLength' => 50, 'required' => true),
            'hook_name'        => array('type' => self::TYPE_STRING, 'maxLength' => 50, 'required' => true),
            'active'        => array('type' => self::TYPE_UINT, 'maxLength' => 1, 'default' => 1),
            'global'        => array('type' => self::TYPE_UINT, 'maxLength' => 1, 'default' => 0),
            'rotate_speed'    => array('type' => self::TYPE_UINT, 'default' => 'null'),
        )
    );
}
 
If you have it as a UINT, then it will indeed convert it to an unsigned integer.

There are other types - I can't actually remember off the top of my head if any of them allow null, or not. Maybe TYPE_UNKNOWN will.

Though, of course, if usually the value is supposed to be an unsigned integer, you will loose some of the type casting it does for that type.

How about an INT field? -1 could be what you're currently using as NULL.

Just a thought, you might want to look into the other field types first.
 
Ah yeah, didn't think of changing it to a signed integer. That seems to be working nicely.

Regards,

Jake
 
Back
Top Bottom