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

How to update SQL entry from DataWriter?

Discussion in 'XenForo Development Discussions' started by Tristan10, Jul 30, 2016.

  1. Tristan10

    Tristan10 Member

    I know you can add entries to a database from the DataWriter by creating a new instance of your DataWriter and the calling set("field_name", $data) . I can't seem to find this "set" function anywhere in order to see if there is an "update" function that is similar.

    I would like to update an entry by indicating which entry to update with the primary key, and then telling it which field to update along with the data. Something like update("Primary_Key_Field", $Primary_Key_To_Update, "field_to_update", $data). Is this possible? Thanks for any help.
  2. Mike

    Mike XenForo Developer Staff Member

    Updates are done with the same method. You call setExistingData some time earlier to identify the element that you're updating (based on the primary key).

    I'd recommend looking in the account controller where various user information is updated. It should give you an idea how the code works.
  3. Tristan10

    Tristan10 Member

    I thought I figured it out, but can't seem to get it working. I tried:

    $dwXboxVerification->set('xbox_gamertag', $text);
    $dwXboxVerification->set('xbox_veriCode', $veriCode);
    $dwXboxVerification->set('xbox_date', $date);
    $dwXboxVerification->set('xbox_xuid', $output);
    $dwXboxVerification->set('xbox_attempts', $userInfo["xbox_attempts"] + 1);


    But returns the following error:
    • The existing data required by the data writer could not be found.
    • Please enter a value for the required field 'xbox_site_id'.
    The 'xbox_site_id' is my primary key. I do not want to update it. I tried "updating" it to the same value, but still didn't work.
  4. Snog

    Snog Well-Known Member

    As Mike said, you set existing data based on the primary key. So...


    Should be...


    And then set the data as needed.
  5. Tristan10

    Tristan10 Member

    Sorry, I messed up when I said my primary key is 'xbox_site_id' . That is the name of the field of the primary key. But the variable $userid is set to its value. Otherwise I could try the value directly like


    Not sure if that would matter.
  6. Snog

    Snog Well-Known Member

    It almost sounds like the primary key you're trying to update doesn't exist.
  7. Liam W

    Liam W Well-Known Member

    Is your getExistingData method setup correctly? It should return an array containing the row of data for each table, keyed by table name.

    protected function _getExistingData($data)
        if (!
    $id $this->_getExistingPrimaryKey($data))

        return array(
    'table_name' => $this->_getModel()->getRecordById($id));
  8. Tristan10

    Tristan10 Member

    Thanks. This led me to see that I had the wrong primary key set in my Model. However, even though everything is executed correctly, the changes still do not appear in the database.
  9. Liam W

    Liam W Well-Known Member

    I'm assuming your getUpdateStatement() method is correct as well? The method name is something like that...
  10. Tristan10

    Tristan10 Member

    Not sure if this is what t is supposed to be..? In the DataWriter I assumed.

    protected function _getUpdateCondition($tableName)
    return 'xbox_site_id = ' . $this->_db->quote($this->getExisting('xbox_site_id'));
  11. Tristan10

    Tristan10 Member

    It seems to be working now. Not sure what I changed, but oh well. Thanks for the help!

Share This Page