I have a strange behaviour using Magento ( 1.4, old version I know) when I save a custom Model.
$tr = Mage::getModel('Middleware/transfer')->load($transfer['transfer_request_id'], 'transfer_request_id');
var_dump(get_class($tr));
var_dump($tr->getfkState());
$tr->setData('fk_state', Middleware_Model_TransferState::TRANSFER_STATE_RECEIVED_MIDDLEWARE);
var_dump($tr->getfkState());
$tr->save();
My var_dump are giving me some good informations, the states changes from 0 initially to 1 ( the value of my TRANSFER_STATE_RECEIVED_MIDDLEWARE constant), and the class is well a Middleware_Model_Transfer.
However, the save tries to do an insert instead of an update, and i ve got the error below
PHP Fatal error: Uncaught exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '11' for key 'PRIMARY'' in magento/lib/Zend/Db/Statement/Pdo.php:234
I totally understand that I have a duplicate key on my transfer_request_id and it is obviously true, but why Magento is trying to insert when I need to update this data I got. Is it possible to force the update if found on a model in Magento?
Thanks