Magento自定义集合具有重复的数据


Magento custom collection has duplicated data

这段代码将使用自定义集合,并使用网格通过Magento的后端(adminhtml)打印文件列表,以便下载或删除。

问题是集合在添加项目($dataCollection->addItem($dataObject))后多次重复,结果相同。

建议?
谢谢
~N

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] => 
)


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
    (
    )
)



法典:

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()));
}

}

发生的情况是,您每次都向集合中添加相同的对象。您需要为每个文件创建一个唯一的Varien_Object。在foreach循环中创建$dataObject,如下所示:

//$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);
}