Magento – “Item (Mage_Catalog_Model_Product) with the same id ”xxx“ already exist”

I have been getting this error when trying to filter a products collection

Item (Mage_Catalog_Model_Product) with the same id "7631" already exist and wanted to ask what could ne causing the error since there is only one (visible) product with the same ID inside of Magento.

———————————————————–

Here is the solution i did to solve the issue.

This works on arbitrary collection, not only for Catalog_Model_Product.

Step 1. Modify the core file lib/Varien/Data/Collection.phpfunction addItem(), but unlikethis answer suggests, don't hide the error.

Instead, add extra error information to the exception thrown:

        if (isset($this->_items[$itemId])) {
            throw new Exception('Item ('.get_class($item).
                ') with the same id "'.$item->getId().'" already exist' .
                '. SQL that caused this: ' . $this->getSelect());
        }

Step 2. Take the offending query from your error report and run it by hand. See what records duplicate the collection key. Add order by <key field> as needed.

Dissect the query removing the participating tables one-by-one, and see which record caused the duplication.

 

In my case it cause by my product collection returns duplicate product ids, so i grouped my product collection by grouping 'e.entity_id'

Here is my sample code

<?php $collection = Mage::getModel('catalog/product')
->getCollection()
->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
->addAttributeToSelect('*')
->addAttributeToFilter('category_id', array('in' => $uniqueIds)); ?>

<?php $collection->getSelect()->group('e.entity_id');?>

 

 

Revisions

2 Responses to “Magento – “Item (Mage_Catalog_Model_Product) with the same id ”xxx“ already exist””

  1. Verena Veale 10/09/2017 at 10:38 pm #

    Hi, Neat post. There is an issue together with your web site in web explorer, would check this… IE still is the marketplace chief and a huge section of people will pass over your wonderful writing because of this problem.

  2. Shaquita Balzano 28/04/2018 at 7:21 pm #

    *It?s hard to find knowledgeable people on this topic, but you sound like you know what you?re talking about! Thanks

Leave a Reply