I need to load a product collection that has repeated products in it. I get this error when trying to load a product collection with repeated products:

Item (Magento\Catalog\Model\Product\Interceptor) with the same ID "1" already exists.
Exception in vendor\magento\framework\Data\Collection.php:407

I am adding to the product collection source data to approve vendors on a custom extension. Since I'm adding the source items table the same sku will have multiple entries. This is fine. I have referenced these posts already but they don't have the answer that applies to my situation:

How to allow duplicate entries in admin grid

Magento 2 | Item with the same ID already exists when join tables

My code is like this:

        $this->collection->getSelect()->joinLeft(['invsourceitem'=>$inventorySourceItemTable],'e.sku = invsourceitem.sku')
        ->joinLeft(['invsource'=>$inventorySourceTable],'invsourceitem.source_code = invsource.source_code')
    ->where('invsourceitem.vendor_approval = 0 AND invsourceitem.quantity > 0');

I also tried aliasing the entity_id like this but then it gives errors that it can't join the EAV tables so that method won't work.

$this->collection->getSelect()->columns(['entity_id_alias'=>'e.entity_id'])->columns(['entity_id'=> 'CONCAT(e.entity_id,invsource.source_code)']);

I have also tried changing the grid xml file primaryFieldName but that doesn't matter since the error is coming from vendor\magento\framework\Data\Collection.php:407

In addition I suppose I'll need some sort of dynamic autoincrement type primary id field for the collection since it doesn't exist in the table, this is a secondary question once the original problem is solved

  • add group by for entity_id column.
    – S.P
    Commented May 18, 2022 at 4:56
  • @S.P thanks but I actually need the products to repeat on the grid, it is an approval grid for products by vendor. There can be multiple vendors for the same product. Commented May 18, 2022 at 4:57
  • you should create custom column to collection, Which has row number , and change entity_id column name to entity_id_original .
    – S.P
    Commented May 18, 2022 at 5:01
  • @S.P Yes I tried that it doesn't work :( that was what I called in my answer 'alias' $this->collection->getSelect()->columns(['entity_id_alias'=>'e.entity_id'])->columns(['entity_id'=> 'CONCAT(e.entity_id,invsource.source_code)']); it gives an error because when the collection joins the EAV fields it has to have the entity_id field Commented May 18, 2022 at 5:30
  • Yes, Can you use (@row_number:=@row_number + 1) AS num at collection and set pricmary key for collection this num column.
    – S.P
    Commented May 18, 2022 at 5:35


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.