cancel
Showing results for 
Search instead for 
Did you mean: 

How to save in database from Model Order

How to save in database from Model Order

I'm trying to keep values ​​in the database by writing a comment from an order (from Administration panel ) .

I have an array with "key " = > "value" of all I want to save . I want to introduce these values ​​within the " sales_flat_order 'table. I've done as follows:

 

$model = Mage::getModel("sales/order")->load($request->getParams()["order_id"]);

This to get the order.

I have tried these four ways, but it doesn't work. Why?

1:

 

$model->addData($data);
    try {
        $model->save();
    } catch (Exception $e){
        echo $e->getMessage();   
    }

2:

$model->setData($key, $value);
 try {
     $model->save();
 } catch (Exception $e){
     echo $e->getMessage();   
 }

 3:

$write_connection = Mage::getSingleton('core/resource')->getConnection('core_write'); 
$query = "UPDATE {$table} SET {$sql} WHERE entity_id = '".$request->getParams()["order_id"]."'";
$writeConnection->query($query);

4:

 $write_connection = Mage::getSingleton('core/resource')->getConnection('core_write'); 
 $where = $write_connection->quoteInto('entity_id =?', $request->getParams()["order_id"]);
 $write_connection->update($table, $data, $where);

Thanks!!

2 REPLIES 2

Re: How to save in database from Model Order

Hello Sergio,

Replace 'addData' with setData as

 

  

model->setData($data);
    try {
        $model->save();
    } catch (Exception $e){
        echo $e->getMessage();   
    }

Re: How to save in database from Model Order

Hi Sergio,

setData() function is only set one field value on one call. it can set multiple field value using multiple call of setData function

addData() function is set multiple field values using array with array key as field index.

$orderid=1;
$orders = Mage::getModel('sales/order');
$orders->setData('column_name','value');
$orders->setId($orderid);
$orders->save();