5

I have a model and I was trying to add a model to the database. When I execute the php script to add everything works fine and no exception are thrown. But the data is not added to the database, which i verified with mysql.

I tried to track the mysql queries and find that in both the cases, setData and addData, Magento is calling an Update command not Insert command. Why is it behaving like this? Is it possible that this behaviour is because of model implementation?

Code snippet to add data -

$data = array('appid'=>'new_appid','custid'=>1,'info'=>'info'); 
$model = Mage::getModel('interface/data')->setData($data); 


try { 
    $insertid = $model->save()->getId(); 
    echo "Data Succesfull inserted. Insert Id : ".$insertid; 
} 
catch (Exception $e){ 
    echo $e->getMessage(); 
}
  • try to remove cache then try.,,,i had same problem occur somethimes ago – Keyur Shah Jun 18 '15 at 7:10
34

This is what you have done here

$data = array('appid'=>'new_appid','custid'=>1,'info'=>'info'); 
$model = Mage::getModel('interface/data')->setData($data); 
$model->save();

Here I am assuming appid is the primary_key of the entiry interface/data.

What is this code ?

Basically what you are doing here is, instantiate your entity, setting some value on that entity and then perform a save action.

What actually happens !

What you are expecting is an INSERTION, but due to the data available to the entity, Magento will try to do an UPDATE by default.

Why It happens so ?

This is because your entity has it's primary_key (ie appid) is already set with your custom value new_appid. Since the primary_key is set, Magento by default, assume you want to perform an UPDATE. So it will try to find a record with appid = 'new_appid'. But it will fail in this operation since such data is not existing in database.

How can you overcome this ?

But there is a way to do an INSERT rather than doing an UPDATE if the primary key is set. To perform this, you need to set _isPkAutoIncrement to false. By default,this value is set to yes which indicates that we need an update if primary key is set. setting this value to false tells to Magento that, I need an INSERT and not an UPDATE. So put this in your model file.

protected $_isPkAutoIncrement = false;
| improve this answer | |
  • 2
    Dude, I feel so sorry for you that your answer is so underrated, you just solved a 1 year waiting problem with this huge fix! Thanks a lot! – Nicolas D Jan 5 '16 at 14:00
  • 4
    @NicolasD That is why stack system provides you provision for upvoting. Do it Dude :) – Rajeev K Tomy Jan 8 '16 at 12:46
  • 1
    @NicolasD .. and Also it is not a fix. It is actually a feature. :) – Rajeev K Tomy Jan 8 '16 at 12:48
3

Magento will do an update, if the current object already has an id set. It will rightfully try and do an UPDATE of an existing record, as designated by the existing ID.

Since I cannot see your resource model definition, or your table definition, I don't know what you have defined as the default row id. (usually this is entity_id)

Is the default id field of your table an auto incremental?

Is any of the fields you are populating with your array, the field you use for auto_increment field in db?

If so, remove that field form the array, which will then cause magento to do an insert.

| improve this answer | |
  • My Table structure ("AppId", "CustId", "ServerId", "Info"). all are text except CustId. which is not auto incremental. Here AppId is the primary key. – ted Jun 17 '15 at 16:30
  • Thanks For the clue on the auto incrementa. I made the _isPkAutoIncrement for db object false and it works fine. – ted Jun 17 '15 at 16:37
0

When user creates a model in magento, it expects the primary key to be an auto increment field called id. If we don't have a auto increment key then we need to need to set the _isPkAutoIncrement flag on the Db object to false.

For a save call - If the flag is false magento will check the table for a row with the default value. If its there it will do an update otherwise it will do an insrt.

| improve this answer | |

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.