How to update SQL entry from DataWriter?

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.
 
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.
 
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.
I thought I figured it out, but can't seem to get it working. I tried:

$dwXboxVerification->setExistingData($userid);
$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);


$dwXboxVerification->save();


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.
 
As Mike said, you set existing data based on the primary key. So...

$dwXboxVerification->setExistingData($userid);


Should be...

$dwXboxVerification->setExistingData($xbox_site_id);

And then set the data as needed.
 
As Mike said, you set existing data based on the primary key. So...

$dwXboxVerification->setExistingData($userid);


Should be...

$dwXboxVerification->setExistingData($xbox_site_id);

And then set the data as needed.
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

$dwXboxVerification->setExistingData($userInfo["xbox_site_id"]);

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

PHP:
protected function _getExistingData($data)
{
    if (!$id = $this->_getExistingPrimaryKey($data))
    {
        return false;
    }

    return array('table_name' => $this->_getModel()->getRecordById($id));
}

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

PHP:
protected function _getExistingData($data)
{
    if (!$id = $this->_getExistingPrimaryKey($data))
    {
        return false;
    }

    return array('table_name' => $this->_getModel()->getRecordById($id));
}

Liam

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

I'm assuming your getUpdateStatement() method is correct as well? The method name is something like that...
 
I'm assuming your getUpdateStatement() method is correct as well? The method name is something like that...
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'));
}
 
Top Bottom