Take the 2-minute tour ×
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It's 100% free, no registration required.

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!

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

3 Answers 3

up vote 4 down vote accepted

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.

share|improve this answer
    
Thanks a lot !!! :) –  user27294 Jun 18 at 12:06

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.

share|improve this answer

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());
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.