2

I have custom affiliates module with grid.this affiliate module grid has list of orders placed by the affiliate customer group. in this grid i had added some columns array('billing_name','created_at','base_grand_total','grand_total','status') from sales order grid and shipment number from the sales_flat_shipment using join in magento.Following is my query.

$collection->join(array('og' =>'sales/order_grid'), 'main_table.order_id = og.entity_id', array('billing_name','created_at','base_grand_total','grand_total','status'));
$collection->getSelect()->joinLeft(array('sfs' =>'sales_flat_shipment'), 'main_table.order_id = sfs.order_id','increment_id as shipment_id');

I am getting this join columns added and data also populated in the affiliate grid with no issue.

The issue is when i am trying to export this grid i am getting the following issue

Item (Neo_Affiliate_Model_Affiliate) with the same id "2977" already exist

When i printed the collection object i see affiliate_id which is primary key of the affiliate module has 2977 twice.so i came to know that if primary key dupliacates then above issue comes.

in the collection i have 2977 duplicate because for order number lets assume 10000011 has 2 shipments so their collection object has 2977 twice with one record as shipment 1 and another record as shipment 2.

i had tryied this issue on google and tried many soultions like

1)$collection->getSelect()->group('main_table.affiliate_id');

2)$collection->getSelect()->distinct(true);

3)$collection->getSelect()->distinct('main_table.affiliate_id');

this issue only coming when i am trying to export.it is not coming when the grid is displaying the records.

Note:- Affiliate table contains records of orders details by affiliates

Updated:-

protected function _prepareCollection()
        {
            $collection = Mage::getModel('neoaffiliate/affiliate')->getCollection();
            $collection->join(array('og' =>'sales/order_grid'), 'main_table.order_id = og.entity_id', array('billing_name','created_at','base_grand_total','grand_total','status'));
            $collection->getSelect()->joinLeft(array('sfs' =>'sales_flat_shipment'), 'main_table.order_id = sfs.order_id','increment_id as shipment_id');

            // tryed code

            //$collection->getSelect()->group('main_table.affiliate_id');
            //$collection->getSelect()->distinct(true);
            //$collection->getSelect()->distinct('main_table.affiliate_id');
            //$collection->getSelect()->distinct();

            $this->setCollection($collection);
            return parent::_prepareCollection();
        }
5
  • In which file did you try above changes? Commented Sep 9, 2015 at 9:43
  • in grid.php which is in the block. (Neo_Affiliate_Block_Adminhtml_Manage_Affiliate_Grid) Commented Sep 9, 2015 at 9:44
  • in _prepareCollection method Commented Sep 9, 2015 at 9:45
  • Isn't primary key supposed to be unique all the time? Commented Sep 9, 2015 at 9:51
  • primary key is unique in the affiliate table.but since because of join and my order has two shipment number for that order thats y that afiiliate order id with 2977 is repeated twice in the collection Commented Sep 9, 2015 at 9:58

1 Answer 1

1

I believe the problem is that for the export the collection is cloned1 and any modifications that you made to the Zend_Db_Select instance directly get lost.

In this case I see two options:

  1. override _exportIterateCollection() to add $collection->getSelect()->distinct(true) to the cloned collection, or
  2. implement __clone() in the collection to always add DISTINCT to clones:

    public function __clone()
    {
        parent::__clone();
        $this->getSelect()->distinct(true);
    }
    

1) see: Mage_Adminhtml_Block_Widget_Grid::_exportIterateCollection

1
  • still not working Commented Sep 10, 2015 at 8:34

Your Answer

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

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