Item with the same id already exist magento collection

item with the same id already exist magento collection

If Magento collection has duplicate id it throws the above exception.
To fix this problem make sure your collection do’nt have the duplicate records.
Check that the collection have single column with the name id(ie. in case of fetching data using joins there should be no duplicate column). Take Alias of the column if same in case of joins.

ex.

  
        $collection = Mage::getModel('test/user')->getCollection()->distinct()
            ->addFieldToSelect('name')
            ->addFieldToSelect('id');
        $collection->getSelect()->join( array('uc'=> user_profile), 'uc.user_id = main_table.id', array('uc.id'));

Should be
>addFieldToSelect(‘id’) field should have alias as ‘userId’ otherwise it will through the exception

  
        $collection = Mage::getModel('test/user')->getCollection()->distinct()
            ->addFieldToSelect('name')
            ->addFieldToSelect('id','userId');
        $collection->getSelect()->join( array('uc'=> user_profile), 'uc.user_id = main_table.id', array('uc.id'));


Advertisements

Add Comment

📖 Read More