Question

This code will use a custom collection and print a listing of files, using a Grid, through Magento's backend(adminhtml) for either downloading or deletion.

The problem is the collection gets duplicated multiple times with the same result after adding an item ( $dataCollection->addItem($dataObject) ).

Suggestions?
Thank you,
~N

Output of d($dataObject):

#first loop
Varien_Object Object
(
[_data:protected] => Array
    (
        [filename] => 20120620_0950_UTC.xml
        [size] => 136740
        [date_modified] => 2012-06-20 14:50:20
    )

[_hasDataChanges:protected] => 1
[_origData:protected] => 
[_idFieldName:protected] => 
[_isDeleted:protected] => 
)


#second loop
Varien_Object Object
(
[_data:protected] => Array
    (
        [filename] => 20120620_0954_UTC.xml
        [size] => 136740
        [date_modified] => 2012-06-20 14:54:41
    )

[_hasDataChanges:protected] => 1
[_origData:protected] => 
[_idFieldName:protected] => 
[_isDeleted:protected] => 
)


Output of d($dataCollection):

Varien_Data_Collection Object
(
[_items:protected] => Array
    (
        [0] => Varien_Object Object
            (
                [_data:protected] => Array
                    (
                        [filename] => 20120620_0954_UTC.xml
                        [size] => 136740
                        [date_modified] => 2012-06-20 14:54:41
                    )

                [_hasDataChanges:protected] => 1
                [_origData:protected] => 
                [_idFieldName:protected] => 
                [_isDeleted:protected] => 
            )

        [1] => Varien_Object Object
            (
                [_data:protected] => Array
                    (
                        [filename] => 20120620_0954_UTC.xml
                        [size] => 136740
                        [date_modified] => 2012-06-20 14:54:41
                    )

                [_hasDataChanges:protected] => 1
                [_origData:protected] => 
                [_idFieldName:protected] => 
                [_isDeleted:protected] => 
            )

    )

[_itemObjectClass:protected] => Varien_Object
[_orders:protected] => Array
    (
    )

[_filters:protected] => Array
    (
    )

[_isFiltersRendered:protected] => 
[_curPage:protected] => 1
[_pageSize:protected] => 
[_totalRecords:protected] => 
[_isCollectionLoaded:protected] => 
[_cacheKey:protected] => 
[_cacheTags:protected] => Array
    (
    )

[_cacheLifetime:protected] => 86400
[_flags:protected] => Array
    (
    )
)



Code:

class Rogue_Googlemerchant_Block_Adminhtml_Exporter_Grid extends Mage_Adminhtml_Block_Widget_Grid
{

public function __construct()
{
    parent::__construct();
    $this->setId('googlemerchantGrid');
    $this->setDefaultSort('entity_id');
    $this->setDefaultDir('ASC');
    $this->setSaveParametersInSession(true);
}

protected function _prepareCollection()
{
    $basePath = Mage::getBaseDir('base');
    $feedPath = $basePath . '/opt/googlemerchant/';
    $errPath = $basePath . '/var/log/googlemerchant/';
    $flocal = new Varien_Io_File();
    $flocal->open(array('path' => $feedPath));
    $dataCollection = new Varien_Data_Collection();
    $dataObject = new Varien_Object();

    foreach ($flocal->ls() as $item) {
        $dataObject->addData(
            array(
                'filename'     => $item['text'],
                'size'         => $item['size'],
                'date_modified'=> $item['mod_date']
            )
        );
        $dataCollection->addItem($dataObject);
        d($dataObject);
    }
    d($dataCollection);
    $this->setCollection($dataCollection);

    return parent::_prepareCollection();
}

protected function _prepareColumns()
{

    $this->addColumn('filename', array(
      'header'    => Mage::helper('googlemerchant')->__('File'),
      'align'     =>'left',
      'index'     => 'filename',
      'width'     => '200px',
    ));

    $this->addColumn('action', array(
        'header'  => Mage::helper('googlemerchant')->__('Action'),
        'width'   => '50px',
        'type'    => 'action',
        'getter'  => 'getId',
        'actions' => array(
            array(
                'caption' => Mage::helper('googlemerchant')->__('Download'),
                'url'     => array('base' => '*/*/download'),
                'field'   => 'entity_id'
            ),
            array(
                'caption' => Mage::helper('googlemerchant')->__('Delete'),
                'url'     => array('base' => '*/*/delete'),
                'field'   => 'entity_id'
            )
        ),
        'filter'    => false,
        'sortable'  => false,
        'index'     => 'stores',
        'is_system' => true,
    ));

    return parent::_prepareColumns();
}

public function getRowUrl($row)
{
    return $this->getUrl('*/*/download', array('entity_id' => $row->getId()));
}

}

Was it helpful?

Solution

What is happening is that you are adding the same object to the collection each time. You'll need to create a unique Varien_Object for each file. Create your $dataObject inside the foreach loop, like this:

//$dataObject = new Varien_Object();

foreach ($flocal->ls() as $item) {
    $dataObject = new Varien_Object();
    $dataObject->addData(
        array(
            'filename'     => $item['text'],
            'size'         => $item['size'],
            'date_modified'=> $item['mod_date']
        )
    );
    $dataCollection->addItem($dataObject);
    d($dataObject);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top