1

Getting fatal error on Item (Mage_Catalog_Model_Product) with the same id "223" already exist

Here 223 is my product Id

My _prepareCollection() Method

protected function _prepareCollection()
{
    $coll = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToFilter('name',array('neq'=>''))  
                ->addAttributeToFilter('attribute_code',$this->getRequest()->getParam('my_value'));


    $coll->getSelect()->join( array('order_item'=> 'mgnt_sales_flat_order_item'), 'order_item.product_id = e.entity_id', array('order_item.created_at as ocreated','order_item.qty_ordered as sales_units', 'order_item.row_total as sales_total' , 'order_item.discount_amount as discount', 'order_item.item_id as item_id' ) );
    $coll->getSelect()->join( array('order'=> 'mgnt_sales_flat_order'), 'order.entity_id = order_item.order_id', array('customer_name'=>'order.customer_firstname'));



     $this->setCollection($coll);

    // echo $coll->getSelect(); 

     return parent::_prepareCollection();
}

My _prepareColumns() method

protected function _prepareColumns()
{ 


    $this->addColumn('entity_id', array(
        'header' => $this->_getHelper()->__('entity_id'),
        'type' => 'text',
        'index' => 'entity_id',
        'filter'    => false,
        'sortable'  => false
    ));
    $this->addColumn('name', array(
        'header' => $this->_getHelper()->__('Product Name'),
        'type' => 'text',
        'index' => 'name',
        'filter'    => false,
        'sortable'  => false
    ));

    $this->addColumn('customer_name', array(
        'header' => $this->_getHelper()->__('customer_name'),
        'type' => 'number',
        'index' => 'customer_name',
        'filter'    => false,
        'sortable'  => false
    ));
    $this->addColumn('sales_total', array(
        'header' => $this->_getHelper()->__('Sales Total'),
        'type' => 'number',
        'index' => 'sales_total',
        'filter'    => false,
        'sortable'  => false
    ));
    $this->addColumn('discount', array(
        'header' => $this->_getHelper()->__('Discount'),
        'type' => 'number',
        'index' => 'discount',
        'filter'    => false,
        'sortable'  => false
    ));



    return parent::_prepareColumns();

}
  • Your collection in okay. check the database if there is any duplicate product Id. – Prasanta Hatui Mar 6 '17 at 5:40
  • @PrasantaHatui nup I echo $coll->getSelect() any query seems to okay what I think this collection then in my result product id repetet so I think that might be issue – Murtuza Zabuawala Mar 6 '17 at 5:43
  • try grouping with product ID, $coll->getSelect()->group('main_table.entity_id'); – Jaimin Sutariya Mar 6 '17 at 5:50
  • @JaiminSutariya I dont wanna use groupby if there is 100 records with same entity_id then I dont have a problem because that is on order_item basis – Murtuza Zabuawala Mar 6 '17 at 5:55
  • 1
    Thanks @GopalPatel that link help me to understand why this error is occured – Murtuza Zabuawala Mar 10 '17 at 12:12
4

As per gopal's suggested link "You added something to the collection, which adds duplicates to the query result. What magento does with the query result is, to generate object from each row and then add these items to the collection. If the item already exists, this error is thrown."

As an hack you can try this for solution in your _prepareCollection() method:

$resource       = Mage::getModel('core/resource');
$connection     = $resource->getConnection('core_read');
$sql            = $coll->getSelect();
$rows           = $connection->fetchAll($sql);//this row will return an array

    $collection = new Varien_Data_Collection();
    foreach($rows as $row){
        $rowObj = new Varien_Object();
        $rowObj->setData($row);
        $collection->addItem($rowObj);
    }

    $this->setCollection($collection);
    return parent::_prepareCollection();
  • 2
    Yeah big thumps up for conversion flow (collection to array and then to object) . Actually i was thinking this step but not thought array as middle ware – Gopal Patel Mar 6 '17 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.