You are viewing an old revision of this post, from January 27, 2016 @ 18:05:30. See below for differences between this version and the current revision.

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

Revision Differences

January 27, 2016 @ 18:05:30Current Revision
Content
Unchanged: <p>Unchanged: <p>
Unchanged: I have been getting this error when trying to filter a products collectionUnchanged: I have been getting this error when trying to filter a products collection
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: <code>Item (Mage_Catalog_ Model_Product) with the same id &quot;7631&quot; already exist</code>&nbsp;and wanted to ask what could ne causing the error since there is only one (visible) product with the same ID inside of Magento.Unchanged: <code>Item (Mage_Catalog_ Model_Product) with the same id &quot;7631&quot; already exist</code>&nbsp;and wanted to ask what could ne causing the error since there is only one (visible) product with the same ID inside of Magento.
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: ------------- ------------- ------------ ---------------------Unchanged: ------------- ------------- ------------ ---------------------
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: Here is the solution i did to solve the issue.Unchanged: Here is the solution i did to solve the issue.
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: This works on arbitrary collection, not only for&nbsp;<code> Catalog_Model_ Product</code>.Unchanged: This works on arbitrary collection, not only for&nbsp;<code> Catalog_Model_ Product</code>.
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: Step 1. Modify the core file&nbsp;<code> lib/Varien/Data/ Collection.php< /code>,&nbsp; <code>function addItem()</code>, but unlike<a href="http:// magento.stackexchange.com/ a/63645/14177">this answer</a>&nbsp;suggests, don&#39;t hide the error.Unchanged: Step 1. Modify the core file&nbsp;<code> lib/Varien/Data/ Collection.php< /code>,&nbsp; <code>function addItem()</code>, but unlike<a href="http:// magento.stackexchange.com/ a/63645/14177">this answer</a>&nbsp;suggests, don&#39;t hide the error.
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: Instead, add extra error information to the exception thrown:Unchanged: Instead, add extra error information to the exception thrown:
Unchanged: </p>Unchanged: </p>
Unchanged: <pre>Unchanged: <pre>
Unchanged: <code> if (isset($this- &gt;_items[$itemId])) {Unchanged: <code> if (isset($this- &gt;_items[$itemId])) {
Unchanged: throw new Exception(&#39;Item (&#39;.get_class($item).Unchanged: throw new Exception(&#39;Item (&#39;.get_class($item).
Unchanged: &#39;) with the same id &quot;&#39;.$item- &gt;getId().&#39;&quot; already exist&#39; .Unchanged: &#39;) with the same id &quot;&#39;.$item- &gt;getId().&#39;&quot; already exist&#39; .
Unchanged: &#39;. SQL that caused this: &#39; . $this-&gt;getSelect());Unchanged: &#39;. SQL that caused this: &#39; . $this-&gt;getSelect());
Unchanged: }Unchanged: }
Unchanged: </code></pre>Unchanged: </code></pre>
Unchanged: <p>Unchanged: <p>
Unchanged: Step 2. Take the offending query from your error report and run it by hand. See what records duplicate the collection key. Add&nbsp;<code>order by &lt;key field&gt;</code>&nbsp;as needed.Unchanged: Step 2. Take the offending query from your error report and run it by hand. See what records duplicate the collection key. Add&nbsp;<code>order by &lt;key field&gt;</code>&nbsp;as needed.
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: Dissect the query removing the participating tables one-by-one, and see which record caused the duplication.Unchanged: Dissect the query removing the participating tables one-by-one, and see which record caused the duplication.
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: &nbsp;Unchanged: &nbsp;
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: In my case it cause by my&nbsp;product collection returns duplicate product ids, so&nbsp;i&nbsp;grouped my product collection by grouping &#39;e.entity_id&#39;Unchanged: In my case it cause by my&nbsp;product collection returns duplicate product ids, so&nbsp;i&nbsp;grouped my product collection by grouping &#39;e.entity_id&#39;
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: Here is my sample codeUnchanged: Here is my sample code
Unchanged: </p>Unchanged: </p>
Deleted: <p> 
 Added: <pre>
 Added: <code>&lt;?php $collection = Mage::getModel( &#39;catalog/ product&#39;)
 Added: -&gt;getCollection()
Deleted: &lt;?php&nbsp; &nbsp;<code>$collection = Mage::getModel( &#39;catalog/ product&#39;) -&gt;getCollection() -&gt;joinField( &#39;category_id&#39;, &#39;catalog/ category_product&#39;, &#39;category_id&#39;, &#39;product_id = entity_id&#39;, null, &#39;left&#39;) -&gt;addAttributeToSelect( &#39;*&#39;) -&gt;addAttributeToFilter( &#39;category_id&#39;, array(&#39;in&#39; =&gt; $uniqueIds)); ?&gt;</code> Added: -&gt;joinField( &#39;category_id&#39;, &#39;catalog/ category_product&#39;, &#39;category_id&#39;, &#39;product_id = entity_id&#39;, null, &#39;left&#39;)
Deleted: </p> 
Deleted: <p>  
 Added: -&gt;addAttributeToSelect( &#39;*&#39;)
 Added: -&gt;addAttributeToFilter( &#39;category_id&#39;, array(&#39;in&#39; =&gt; $uniqueIds)); ?&gt;
Deleted: <code>&lt;?php $collection-&gt; getSelect()-&gt;group(&#39; e.entity_id&#39; );?&gt;</code> Added: &lt;?php $collection-&gt; getSelect()-&gt;group(&#39; e.entity_id&#39; );?&gt;</code></pre>
Deleted: </p> 
Deleted: <p> 
Deleted: &nbsp; 
Deleted: </p> 
Unchanged: <p>Unchanged: <p>
Unchanged: &nbsp;Unchanged: &nbsp;
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: &nbsp;Unchanged: &nbsp;
Unchanged: </p>Unchanged: </p>

Note: Spaces may be added to comparison text to allow better line wrapping.

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