1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Issue with extending a DataWriter

Discussion in 'XenForo Development Discussions' started by NixFifty, Feb 3, 2014.

  1. NixFifty

    NixFifty Well-Known 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:
    #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:
    $dw = XenForo_DataWriter::create('XenForo_DataWriter_User');
    $dw->set('tokens', $payUser['tokens'] + 1);
    Finally, here's what I'm extending to the DataWriter:
    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 :)
  2. Chris D

    Chris D XenForo Developer Staff Member

    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.
  3. NixFifty

    NixFifty Well-Known Member

    My Listener:
    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:
  4. JulianD

    JulianD Well-Known Member

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

    Syndol Guest

    Have you added the column 'tokens' to the user table?
  6. Chris D

    Chris D XenForo Developer Staff Member

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

    NixFifty Well-Known Member

    I've used pretty much the same code elsewhere and it works fine so I'm not understanding why this breaks.

    Yes. I use another bit of code to add to the same column and that works without a problem.

    Any other possible causes as to why it doesn't reach that far?

    Thanks for the help so far.
  8. Chris D

    Chris D XenForo Developer Staff Member

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

            $dw XenForo_DataWriter::create('XenForo_DataWriter_User');
    ...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?
  9. NixFifty

    NixFifty Well-Known Member

    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.
  10. NixFifty

    NixFifty Well-Known Member

    No joy :(
  11. NixFifty

    NixFifty Well-Known Member

    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.

Share This Page