Issue with extending a DataWriter

Naz

XenForo developer
Staff member
I seem to be having a bit of trouble extending the User DataWriter. Whenever the particular code is executed, I get this in the server error log:

XenForo_Exception: The field 'tokens' was not recognised. - library/XenForo/DataWriter.php:1333

Stack Trace:
Code:
#0 /home/se7ensin/public_html/forums/library/XenForo/DataWriter.php(1375): XenForo_DataWriter->_haveErrorsPreventSave()
#1 /home/se7ensin/public_html/forums/library/NixFifty/UpgradesPlus/Model/Upgrades.php(465): XenForo_DataWriter->save()
#2 /home/se7ensin/public_html/forums/library/NixFifty/UpgradesPlus/Model/PayPal.php(255): NixFifty_UpgradesPlus_Model_Upgrades->upgradeUser('649260', '478168', Array)
#3 /home/se7ensin/public_html/forums/gift_callback.php(31): NixFifty_UpgradesPlus_Model_PayPal->processTransaction()
#4 {main}

Code that's on or near line 465 in Upgrades.php:
Code:
$dw = XenForo_DataWriter::create('XenForo_DataWriter_User');
$dw->setExistingData($payUser);
$dw->set('tokens', $payUser['tokens'] + 1);
$dw->save();

Finally, here's what I'm extending to the DataWriter:
Code:
protected function _getFields()
    {
        $result = parent::_getFields();
        $result['xf_user']['tokens'] = array('type' => self::TYPE_FLOAT, 'default' => 0);
        return $result;
    }

Could someone point out the problem please? Thank you :)
 
The error suggests that this bit isn't working:

Code:
protected function _getFields()
    {
        $result = parent::_getFields();
        $result['xf_user']['tokens'] = array('type' => self::TYPE_FLOAT, 'default' => 0);
        return $result;
    }

I recommending double checking to ensure your code event listener is set up correctly, uses the correct event hint. Feel free to post a screenshot of that and your Listener PHP file.
 
The error suggests that this bit isn't working:



I recommending double checking to ensure your code event listener is set up correctly, uses the correct event hint. Feel free to post a screenshot of that and your Listener PHP file.
My Listener:
Code:
class NixFifty_UpgradesPlus_Listener_DataWriter
{
    public static function loadClassUser($class, &$extend)
    {
        if ($class == 'XenForo_DataWriter_User')
        {
            $extend[] = 'NixFifty_UpgradesPlus_DataWriter_User';
        }
    }
}

And the Code Event Listener:
12db2b496dc8ba9069d1f2d7c0cfc9b3.png
 
The way I see it, you need to instruct the datawriter that 'tokens' is a valid field. Otherwise, it will fail. The datawriter fetches all valid fields from the _getFields() method.
 
The way I see it, you need to instruct the datawriter that 'tokens' is a valid field. Otherwise, it will fail. The datawriter fetches all valid fields from the _getFields() method.
He's done that.

Have you added the column 'tokens' to the user table?
It's a DataWriter error, not a MySQL error. It appears as though the DataWriter isn't getting that far.

That being said, both valid questions because from what I can tell so far all of the code looks sound...
 
The way I see it, you need to instruct the datawriter that 'tokens' is a valid field. Otherwise, it will fail. The datawriter fetches all valid fields from the _getFields() method.
I've used pretty much the same code elsewhere and it works fine so I'm not understanding why this breaks.

Have you added the column 'tokens' to the user table?
Yes. I use another bit of code to add to the same column and that works without a problem.

He's done that.


It's a DataWriter error, not a MySQL error. It appears as though the DataWriter isn't getting that far.

That being said, both valid questions because from what I can tell so far all of the code looks sound...
Any other possible causes as to why it doesn't reach that far?

Thanks for the help so far.
 
My only thought is if you have some other add-on installed that might be doing a bad job of extending that function.

If you use this code on any page that renders a view...

PHP:
        $dw = XenForo_DataWriter::create('XenForo_DataWriter_User');
        Zend_Debug::dump($dw->getFields());

...and go to that page, does your tokens field appear in the output?

Do you get the same error even if you disable all other add-ons?
 
My only thought is if you have some other add-on installed that might be doing a bad job of extending that function.

If you use this code on any page that renders a view...

PHP:
        $dw = XenForo_DataWriter::create('XenForo_DataWriter_User');
        Zend_Debug::dump($dw->getFields());

...and go to that page, does your tokens field appear in the output?

Do you get the same error even if you disable all other add-ons?
The token field does appear in the output.

Just going to go disable all add-ons on the staging site we have and see if that causes the same issue.
 
My only thought is if you have some other add-on installed that might be doing a bad job of extending that function.

If you use this code on any page that renders a view...

PHP:
        $dw = XenForo_DataWriter::create('XenForo_DataWriter_User');
        Zend_Debug::dump($dw->getFields());

...and go to that page, does your tokens field appear in the output?

Do you get the same error even if you disable all other add-ons?
No joy :(
 
Bit of an update. Couldn't get the DataWriter working so I just went and did it with a SQL query instead. Worked like a charm of course.
 
Top Bottom