0

如何将 _prepareCollection 和 _prepareColumn 从核心覆盖到本地。我想在网格中添加新列。这个怎么做?

protected function _prepareCollection()
  {
      $collection = Mage::getModel('players/players')->getCollection();
      $this->setCollection($collection);
      return parent::_prepareCollection();
  }


protected function _prepareColumns()
  {
      $this->addColumn('player_id', array(
          'header'    => Mage::helper('players')->__('ID'),
          'align'     =>'right',
          'width'     => '50px',
          'index'     => 'player_id',
      ));

      $this->addColumn('name', array(
          'header'    => Mage::helper('players')->__('Name'),
          'align'     =>'left',
          'index'     => 'name',
      ));    

      return parent::_prepareColumns();
  }
4

1 回答 1

8

那么有两种方法可以做到这一点。第一个虽然不是首选,但更简单的是“本地包含破解”它并将网格从 core/Mage/../.. 移动到本地/Mage../.. 并简单地进行所需的更改。

另一种方法是重写模块 config.xml 中的文件:

    <blocks>
        <customadminhtml>
            <class>Namespace_CustomAdminhtml_Block</class>
        </customadminhtml>
        <adminhtml>
            <rewrite>
                <sales_order_grid>Namespace_CustomAdminhtml_Block_Adminhtml_Sales_Order_Grid</sales_order_grid>
            </rewrite>
        </adminhtml>
    </blocks>

在重写的文件中,我不会试图覆盖“_prepareCollection”调用。您将如何调用它并设置集合,从逻辑上讲,您无法在其中正确插入更改并仍保持原始功能。相反,我会覆盖“setCollection”方法。通过这样做,您可以维护原始 _prepareCollection 函数的逻辑,并且可以将您的逻辑插入到流程中:

/**
* @brief Add customer_email to sales order
* @param Mage_Sales_Model_Resource_order_grid_collection $collection
*/
public function setCollection($collection)
{
    /** @var Mage_Eav_Model_Config $eav */
    $eav       = Mage::getModel('eav/config');
    $attribute = $eav->getAttribute('customer', 'customer_number');

    $connection = $collection->getConnection();
    $collection->join(array('sfo' => 'sales/order'), 'main_table.entity_id=sfo.entity_id', 'customer_email');

    if ($attribute->getId()) {
        $collection->getSelect()
            ->joinLeft(array('c' => $connection->getTableName('customer_entity_varchar')),
                       'main_table.customer_id = c.entity_id AND c.attribute_id = '.$attribute->getId(),
                       array('customer_number' => 'value'));
    }

    parent::setCollection($collection);
}

最后,您可以通过覆盖普通的“_prepareColumns”函数来添加列,只需事先调用父级:

public function _prepareColumns()
{
    parent::_prepareColumns();

    $this->addColumnAfter('customer_email', array(
                                                 'header' => Mage::helper('customer')->__('Customer Email'),
                                                 'width'  => '50px',
                                                 'index'  => 'customer_email',
                                            ), 'shipping_name');

    $this->sortColumnsByOrder();
    return $this;
}
于 2013-07-02T19:17:18.747 回答