Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update in sales order table - Magento Model

Tags:

magento

I want to update one field value in magento I am using

$data = array('xxx'=>$xxx);
$orderModel = Mage::getModel('sales/order')->load($orderId)->addData($data);
try {
    $orderModel->setId($orderId)->save();
    echo "Data updated successfully.";
} catch (Exception $e){
    echo $e->getMessage();
}

But it is not working, Any one can suggest me what I am doing the create problem in updating data.

like image 210
Kamesh Jungi Avatar asked Dec 18 '25 03:12

Kamesh Jungi


2 Answers

you can do the MAGIC function, get and set function

try directly:

// if your attribute is column named is_active then you can getIsActive() or setIsActive(1)
$orderModel = Mage::getModel('sales/order')->load($orderId)->setYourAttribute($data);
// it means you can set and get column named by ['your_attribute']
try {
    $orderModel->setId($orderId)->save();
    echo "Data updated successfully.";
} catch (Exception $e){
    echo $e->getMessage();
}

hmmm. i don't know why you set the entity_id of sales/order model. but it won't work because the entity_id can be the foreign key on another table, example : sales_flat_order_payment

like image 87
Josua Marcel C Avatar answered Dec 20 '25 00:12

Josua Marcel C


Your code setId($orderId) looks like you either want to explicitely set an order entity_id (that is, create an order), or you're just not aware of the fact, that you don't need setId($orderId) after loading, if you only want to update the given order.

In case you're trying to create an order: the sales/order model normally does not allow to explicitely set an order's entity_id, because it uses a primary key being auto-incremented by default.

In case you're trying to update an existing order: remove the setId($orderId) from the save() chain.

Second, you need to extend the sales/order model with an xxx attribute first, if you want to be able to save its value to the database.

There are several ways to extend the sales/order model to have a custom attribute. For example you could have your own setup script in your app/code/local/Mycompany/Mymodule/sql/myresource/ folder:

// Mage_Eav_Model_Entity_Setup
$oInstaller = $this;

// Add EAV attribute
$oInstaller->startSetup();
$oInstaller->addAttribute(
    'order',
    'my_attribute',
    array(
        'type' => 'int'
    )
);
$oInstaller->endSetup();

// Add flat attribute
if (Mage::getVersion() >= 1.1) {
    $oInstaller->startSetup();
    $oConnection = $oInstaller->getConnection();
    $oConnection->addColumn(
        $oInstaller->getTable('sales_flat_order'),
        'my_attribute',
        'TINYINT(1) NOT NULL'
    );
    $oInstaller->endSetup();
}
like image 28
Jürgen Thelen Avatar answered Dec 19 '25 23:12

Jürgen Thelen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!