4

in my own module I want to INSERT the orderId to my own DB Table.

$data = array( 'order_id' => $order->getId() );
$model = Mage::getModel('magic/test')->setData($data);
try {
    $model->save();
    Mage::getSingleton('adminhtml/session')->addSuccess('Success');
} catch (Exception $e){ 
    Mage::getSingleton('adminhtml/session')->addError('Error'.$e->getMessage());
}

But I get this Exception:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE (order_id='8606')' at line 1, query was: UPDATE magic_test SET WHERE (order_id='8606')

my table Structure

CREATE TABLE IF NOT EXISTS `magic_test` ( `order_id` int(10) unsigned NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;`


ALTER TABLE `magic_test`
 ADD PRIMARY KEY (`order_id`), ADD UNIQUE KEY `order_id` (`order_id`);

I want to insert data but why Magento makes a UPDATE SQL query?

I trying to delete some data with

$model = Mage::getModel('magic/test');
$model->setId('123456')->delete();

This works like a charm!

  • I believe you already have a row with 8606 in it. – Adarsh Khatri Jun 18 '15 at 11:52
  • No the table is empty. – user27294 Jun 18 '15 at 11:53
  • is this PK order_id – Qaisar Satti Jun 18 '15 at 11:54
  • Yes. Add my table Structure.. – user27294 Jun 18 '15 at 11:58
4

The model object does not hold any data besides the ID. Magento does not expect this and still tries to build an update query, which is invalid as you can see in the error message:

UPDATE magic_test SET WHERE (order_id='8606')
                     ^
                    !!!

Further clarification: you specified the primary key order_id as "id column" in Magento. An object with a non empty ID is treated as an existing database entry which needs to be updated. An insert only happens if the ID is null.

You should add a second column "id" as artificial primary key which you specify as id column in your model class.

2

Try this way

$data = array( 'order_id' => $order->getId() );

$model = Mage::getModel('magic/test');

try {
    foreach($data as $id){
       $model->setId($this->getRequest()->getParam('id'))
             ->setOrderId($id);
    }
    $model->save();
    Mage::getSingleton('adminhtml/session')->addSuccess('Success');
} catch (Exception $e){ 
    Mage::getSingleton('adminhtml/session')->addError('Error'.$e->getMessage());
}

My assumption is your column name is order_id and you also have identification column as primary key for your table.

1

Try this :

$model = Mage::getModel('magic/test')->setOrderId($order->getId());
try {
    $model->save();
    Mage::getSingleton('adminhtml/session')->addSuccess('Success');
} catch (Exception $e){ 
        Mage::getSingleton('adminhtml/session')->addError('Error'.$e->getMessage());
}

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.