我在管理员客户网格中添加了“公司”字段,但是我面临的问题是不起作用。

我已经覆盖mage_adminhtml_block_customer_grid类,并在下面添加了代码:

 protected function _prepareCollection()
    {
        parent::_prepareCollection();

        $collection = Mage::getResourceModel('customer/customer_collection')
            ->addNameToSelect()
            ->addAttributeToSelect('email')
            ->addAttributeToSelect('created_at')
            ->addAttributeToSelect('group_id')
            ->addAttributeToSelect('company')
            ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
            ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
            ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
            ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
            ->joinAttribute('billing_company', 'customer_address/company', 'default_billing', null, 'left')
            ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left'); 

        $this->setCollection($collection); 

        return $this;
    }
    protected function _prepareColumns()
    {


        $this->addColumn('company', array(
            'header'    =>  Mage::helper('customer')->__('Company'),
            'width'     =>  '150',
            'index'     =>  'billing_company',

        )); 
        $this->addColumnsOrder('billing_company', 'email');
        return parent::_prepareColumns();
    }

谁能有上述问题的解决方案?

有帮助吗?

解决方案

问题是Magento在收藏中做了很多必需的工作 Mage_Adminhtml_Block_Widget_Grid::_prepareCollection(), ,也就是说,当您打电话时 parent::_prepareCollection() 在覆盖方法的开头。
发生的是

  1. 你打电话给原始父母 _prepareCollection()
  2. 原始集合是初始化的
    Mage_Adminhtml_Block_Customer_Grid::_prepareCollection()
  3. 该集合分配给网格
    $this->setCollection()
  4. 抽象的家长网格 _prepareCollection() 叫做。
  5. 在此方法调用期间,应用订单,在此时间点是默认顺序,因为该集合尚未获得 company 或者 billing_company 它的选择列表中的属性。
    $this->_setCollectionOrder($this->_columns[$columnId]);
  6. 父母之后 _prepareCollection() 方法完成,您可以实例化新的客户集合,并添加您要显示的所有属性,并将其分配给网格。
  7. 该订单未重新应用,因为您不调用抽象网格 _prepareCollection 再次。

你不能打电话 parent::_prepareCollection() 将再次用原始收藏品覆盖您的收藏。

一种可能的解决方案是从抽象网格中复制逻辑 _prepareCollection() 方法,但是复制代码总是不适合可维护性。

一个更好的解决方案是寻找合适的挂钩方法,使您有机会将新公司专栏添加到 现存的 收藏 应用排序顺序和过滤。

查看前几行 Mage_Adminhtml_Block_Widget_Grid::_prepareCollection() 我们可以看到我们需要的东西:

protected function _prepareCollection()
{
    if ($this->getCollection()) {

        $this->_preparePage();

因此,而不是覆盖 protected function _prepareCollection() 在您的块中,请尝试以下操作:

protected function _preparePage()
{
    $this->getCollection()
        ->joinAttribute('billing_company', 'customer_address/company', 'default_billing', null, 'left');
    return parent::_preparePage();
}

这都是时间的问题,并在过程流程期间在正确点插入自定义逻辑。

许可以下: CC-BY-SA归因
scroll top