1

What I have

I've created a grid in admin area which fills with this collection:

protected function _prepareCollection() {
    $collection = Mage::getResourceModel('mymodule/mymodel_collection')
        ->addFieldToFilter('ce2.attribute_id', array('eq' => array(5)))
        ->addFieldToFilter('ce3.attribute_id', array('eq' => array(7)))
        ->addFieldToFilter('ce4.attribute_id', array('eq' => array(862)))
        ->join(array('customerTable' => 'customer/entity'),
            'customer_id=entity_id',
            array('entity_id', 'email')
        );

    $collection->getSelect()
        ->join( array('ce2' => 'customer_entity_varchar'), 'ce2.entity_id=customerTable.entity_id', array('customer_first_name' => 'value'))
        ->join( array('ce3' => 'customer_entity_varchar'), 'ce3.entity_id=customerTable.entity_id', array('customer_last_name' => 'value'))
        ->join( array('ce4' => 'customer_entity_varchar'), 'ce4.entity_id=customerTable.entity_id', array('customer_store_name' => 'value'));

    $this->setCollection($collection);

    parent::_prepareCollection();

    return $this;
}

I've prepared some columns in _prepareColumns and I've set it up to manage export the rows in CSV format:

protected function _prepareColumns() {
    ...

    $this->addExportType('*/*/exportCsv', Mage::helper('customer')->__('CSV'));
    return parent::_prepareColumns();
}

And in my controller class I have nothing but generating CSV:

public function exportCsvAction()
{
    $fileName = 'export.csv';
    $grid = $this->getLayout()->createBlock('mymodule/adminhtml_myblock_grid');
    $this->_prepareDownloadResponse($fileName, $grid->getCsvFile());
}

The Problem

The problem is I'm getting Item (Myvendor_MyModule_Model_Mymodel) with the same id "{id}" already exist, whenever I hit the export button.

What I've Found

I've tracked down the issue in lib/Varien/Data/Collection.php:367:

public function addItem(Varien_Object $item)
{
    $itemId = $this->_getItemId($item);

    if (!is_null($itemId)) {
        if (isset($this->_items[$itemId])) {
            throw new Exception('Item ('.get_class($item).') with the same id "'.$item->getId().'" already exist');
        }
        $this->_items[$itemId] = $item;
    } else {
        $this->_addItem($item);
    }
    return $this;
}

And looking into $this->_getItemId($item) I can see that it's just getting the item's id with getId() magic method:

/**
 * Retrieve item id
 *
 * @param Varien_Object $item
 * @return mixed
 */
protected function _getItemId(Varien_Object $item)
{
    return $item->getId();
}

The Question

I think all I need to do is setting the id of each item in my collection, which I don't know how. Do you have any ideas?

More Info

Here's the definition of my table in database:

+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| customer_id | int(11) unsigned | NO   |     | NULL    |                |
| login_date  | datetime         | NO   |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+
  • This is because you have duplicate Ids in the collection, try with a group by id will resolve your problem. Grid will expect primary key as unique. Try to print your query in plain SQL and see the results. – Rinto George Jul 29 '18 at 5:55
  • Thanks. I know that. My question is how can I set the default ID to my table id primary key. Now it seems the default ID is the customer's ID. – Pedram Behroozi Jul 31 '18 at 6:04

Your Answer

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

Browse other questions tagged or ask your own question.