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!

Fabian Schmengler
66.2k25 gold badges191 silver badges422 bronze badges
asked Jun 18, 2015 at 11:46
4
  • I believe you already have a row with 8606 in it. Commented Jun 18, 2015 at 11:52
  • No the table is empty. Commented Jun 18, 2015 at 11:53
  • is this PK order_id Commented Jun 18, 2015 at 11:54
  • Yes. Add my table Structure.. Commented Jun 18, 2015 at 11:58

3 Answers 3

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.

answered Jun 18, 2015 at 11:57
0
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.

answered Jun 18, 2015 at 11:57
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());
}
answered Jun 18, 2015 at 11:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.