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!
Best Answer
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:
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.