2

此代码将使用自定义集合并使用网格打印文件列表,通过 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()));
}

}

4

1 回答 1

0

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

//$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);
}
于 2012-06-20T20:02:03.373 回答