如何将_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();
}
答案 0 :(得分:7)
有两种方法可以做到这一点。第一个,虽然更容易不是首选,是“本地包括黑客”它并将网格从core / Mage /../ ..移动到local / 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;
}