Question

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(); 
}
Was it helpful?

Solution

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;

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top