0

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?

1

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;
}
2
  • Yeah. It worked! I was thinking about that, but then thought "ok, setData() has 2 arguments - key and value". So, as I understand - when I pass it an array - it will automatically treat each key+value of the array as separate save execution, but will do it simultaneousl?
    – Syspect
    Feb 26 '14 at 15:43
  • @Syspect. I added the setData method with my comments to make it clerer.
    – Marius
    Feb 26 '14 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.