Magento 1.7 – Save Two Columns in One Row with setData()

databasemagento-1.7MySQL

How can I save 2 values of a row in a table, simultaneously?
The table is called entity_export and the columns are entity_id and entity_type.

Here is what I have:

Mage::getModel('xmlexport/export_entity')->setData('entity_id' => $orderNumber, 'entity_type' => 'order')->save();

but I get syntax error on the first =>. And it looks like setData() can save only 1 column value at a time, but I need them 2 simultaneously.

This is the direct SQL query execution that I was using before:

$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$sql = 'INSERT INTO  `export_entity` (
        `id` ,
        `entity_id` ,
        `entity_type`
        )
        VALUES (
        NULL ,  "'.$orderNumber.'",  "order"
        )';
$connection->query($sql);

But I want to make it more elegant and injection proof. That's why I am trying to use the provided functionality by the framework, but I can't seem to handle it correctly. Any suggestions?

Best Answer

Try this:

Mage::getModel('xmlexport/export_entity')->setData(array('entity_id' => $orderNumber, 'entity_type' => 'order'))->save();

You forgot to wrap your values inside an array.

Here is how Varien_Object::setData() looks like

public function setData($key, $value=null)
{
    $this->_hasDataChanges = true;
    if(is_array($key)) { //if the $key is an array just set that as _data
        $this->_data = $key;  
        $this->_addFullNames(); //do something not important
    } else { //if not array
        $this->_data[$key] = $value; //set data for a specific key
        //do other stuff here
        if (isset($this->_syncFieldsMap[$key])) { 
            $fullFieldName = $this->_syncFieldsMap[$key];
            $this->_data[$fullFieldName] = $value;
        }
    }
    return $this;
}