在magento中创建自定义集合

时间:2014-03-12 10:32:13

标签: magento collections adminhtml

我们有一个包含交货日期的表格,我想将其添加到sales / order / grid中的adminhtml。 我遇到的问题是左连接示例对我不起作用,而且我没有选择。

Mage/Adminhtml/Block/Sales/Order/Grid.php

我现在尝试的方法是在_prepareCollection()中创建自定义查询

    $resource = Mage::getSingleton('core/resource');
    $readConnection = $resource->getConnection('core_read');

    $query = 'SELECT * FROM mage_sales_flat_order_grid
              left join mage_aw_deliverydate_delivery
              on mage_sales_flat_order_grid.entity_id =  
              mage_aw_deliverydate_delivery.order_id';

    $collection = $readConnection->fetchAll($query);

   //$collection = Mage::getResourceModel($this->_getCollectionClass());
   //$this->setCollection($collection);
   return parent::_prepareCollection();

然后我在_prepareColumns()中添加此列代码

    $this->addColumn('delivery_date', array(
        'header' => 'delivery_date',
        'index' => 'delivery_date',
        'type' => 'datetime',
        'width' => '100px',
    ));

当我查看后台的订单网格时,我甚至都看不到新列。

感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

如果要添加另一个表

protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel($this->_getCollectionClass());

       $collection->getSelect()->joinLeft('aw_deliverydate_delivery', 'main_table.entity_id = aw_deliverydate_delivery.order_id',array('*' ) );
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

以下代码在_prepareColumns()

 $this->addColumn('aw_deliverydate_delivery', array(
            'header' => Mage::helper('sales')->__('Delivery Date '),
            'index' => 'aw_deliverydate_delivery',
            'type' => 'datetime',
            'width' => '100px',

        ));