Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrity constraint violation error when saving an existing Configurable Item

I am importing items into Magento as configurable items. When I am creating the stock item everything is OK but when the item is being updated it produces this error:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '315-315' for key 'UNQ_TEST1_CATALOG_PRODUCT_SUPER_LINK_PRODUCT_ID_PARENT_ID'

The code block I am using is this:

//Try and open for edit.
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $stockCode);            

//Fetch data from XML and update stock item.
$productData = $this->getProductDataArray($stockItem, $stockCategoryList);
$this->saveStockItem($stockItem, $product, 'configurable', $setId, $productData);


//Reopen the stock item to get any changes.
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $stockCode);

//Build a list of existing attributes for this config product so we can check existing attributes etc.
$existingConfigAttributes = $this->getExistingConfigurableAttributes($product);
$configurableProductsData = $this->getConfigurableProductData($product, $existingConfigAttributes);
$configurableAttributesData = $this->getConfigurableAttributeProductData($product, $existingConfigAttributes);

//Set the configurable attribute information
if ($configurableProductsData != null)
    $product->setConfigurableProductsData($configurableProductsData);
if ($configurableAttributesData != null)
    $product->setConfigurableAttributesData($configurableAttributesData);
$product->setCanSaveConfigurableAttributes(1);
$product->save(); 

Everything is working fine the first time through and my configurable item is working as expected. However, if I run it again, it falls over on the $product->save();. I am assuming it's trying to treat this as a new item rather than an existing one and hence trying to insert where it shouldn't.

$this->saveStockItem() contains:

$stockCode = (string)$stockItem->STOCK_CODE;
if ($product)
{
    $productId = $product->getId();
}

$mc = new Mage_Catalog_Model_Product_Api();
if ($this->validId($productId))
{
    $mc->update($productId, $productData);
    return $productId;
}
else
{
    return $mc->create($type, $setId, $stockCode, $productData);
}

I know I can update an item like this but this won't update the extra attributes I need to add to a configurable item.

What is the correct way to update this information?

like image 377
webnoob Avatar asked Jan 27 '26 11:01

webnoob


1 Answers

Sorry, can't comment (?), so I have to fill in the answer field.

  1. I don't think that Magento tries to treat the product as a new one; load by sku and then save is a normal procedure, worked for me in many cases.

What the error message is trying to say, I guess, is that you are trying to add an item as a child with a value in any configurable attribute that is already there; for example, add a shirt with super-attribute "size" and "size=S" to a configurable product that already has an "S" applied. So, maybe Magento is treating the children as new - not the configurable product.

  1. The key in your error message looks "weird". Does it look familiar to you from one of your other functions? I don't recognize it from any magento table.

  2. What keeps you from using the super-simple and lightning-fast magmi importer? ;) http://sourceforge.net/projects/magmi/

Regards Simon

like image 161
simonthesorcerer Avatar answered Jan 30 '26 00:01

simonthesorcerer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!