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.php
, function 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
- January 27, 2016 @ 18:15:34 [Current Revision] by admin
- January 27, 2016 @ 18:15:34 by admin
- January 27, 2016 @ 18:05:30 by admin
Revision Differences
January 27, 2016 @ 18:05:30 | Current Revision | ||
---|---|---|---|
Content | |||
Unchanged: <p> | Unchanged: <p> | ||
Unchanged: I have been getting this error when trying to filter a products collection | Unchanged: 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 "7631" already exist</code> 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 "7631" already exist</code> 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 <code> Catalog_Model_ Product</code>. | Unchanged: This works on arbitrary collection, not only for <code> Catalog_Model_ Product</code>. | ||
Unchanged: </p> | Unchanged: </p> | ||
Unchanged: <p> | Unchanged: <p> | ||
Unchanged: Step 1. Modify the core file <code> lib/Varien/Data/ Collection.php< /code>, <code>function addItem()</code>, but unlike<a href="http:// magento.stackexchange.com/ a/63645/14177">this answer</a> suggests, don't hide the error. | Unchanged: Step 1. Modify the core file <code> lib/Varien/Data/ Collection.php< /code>, <code>function addItem()</code>, but unlike<a href="http:// magento.stackexchange.com/ a/63645/14177">this answer</a> suggests, don'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- >_items[$itemId])) { | Unchanged: <code> if (isset($this- >_items[$itemId])) { | ||
Unchanged: throw new Exception('Item ('.get_class($item). | Unchanged: throw new Exception('Item ('.get_class($item). | ||
Unchanged: ') with the same id "'.$item- >getId().'" already exist' . | Unchanged: ') with the same id "'.$item- >getId().'" already exist' . | ||
Unchanged: '. SQL that caused this: ' . $this->getSelect()); | Unchanged: '. SQL that caused this: ' . $this->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 <code>order by <key field></code> 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 <code>order by <key field></code> 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: | Unchanged: | ||
Unchanged: </p> | Unchanged: </p> | ||
Unchanged: <p> | Unchanged: <p> | ||
Unchanged: In my case it cause by my product collection returns duplicate product ids, so i grouped my product collection by grouping 'e.entity_id' | Unchanged: In my case it cause by my product collection returns duplicate product ids, so i grouped my product collection by grouping 'e.entity_id' | ||
Unchanged: </p> | Unchanged: </p> | ||
Unchanged: <p> | Unchanged: <p> | ||
Unchanged: Here is my sample code | Unchanged: Here is my sample code | ||
Unchanged: </p> | Unchanged: </p> | ||
Deleted: <p> | |||
Added: <pre> | |||
Added: <code><?php $collection = Mage::getModel( 'catalog/ product') | |||
Added: ->getCollection() | |||
Deleted: | Added: ->joinField( 'category_id', 'catalog/ category_product', 'category_id', 'product_id = entity_id', null, 'left') | ||
Deleted: </p> | |||
Deleted: < | |||
Added: ->addAttributeToSelect( '*') | |||
Added: ->addAttributeToFilter( 'category_id', array('in' => $uniqueIds)); ?> | |||
Deleted: | Added: <?php $collection-> getSelect()->group(' e.entity_id' );?></code></pre> | ||
Deleted: </p> | |||
Deleted: <p> | |||
Deleted: | |||
Deleted: </p> | |||
Unchanged: <p> | Unchanged: <p> | ||
Unchanged: | Unchanged: | ||
Unchanged: </p> | Unchanged: </p> | ||
Unchanged: <p> | Unchanged: <p> | ||
Unchanged: | Unchanged: | ||
Unchanged: </p> | Unchanged: </p> |
Note: Spaces may be added to comparison text to allow better line wrapping.
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.
*It?s hard to find knowledgeable people on this topic, but you sound like you know what you?re talking about! Thanks