在Magento Customer Grid中添加新列customer_id没有索引值

时间:2014-03-22 02:10:02

标签: magento

在Magento Customer Grid中添加新列customer_id没有索引值。

我在Magento客户网格中添加新列customer_id时遇到问题,导致没有索引值。该列为空。

这是我的代码:

<?php
   class Mage_Adminhtml_Block_Customer_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
   public function __construct()
    {
     parent::__construct();
    $this->setId('customerGrid');
    $this->setUseAjax(true);
    $this->setDefaultSort('entity_id');
    $this->setSaveParametersInSession(true);
    }   

 protected function _prepareCollection()
   {
    $collection = Mage::getResourceModel('customer/customer_collection')        
        ->addNameToSelect()             
        ->addAttributeToSelect('email')
        ->addAttributeToSelect('created_at')
        ->addAttributeToSelect('group_id')  
        ->addAttributeToSelect('customer_id')           
        ->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_country_id', 'customer_address/country_id', 'default_billing', null, 'left');
    $this->setCollection($collection);

    return parent::_prepareCollection();
}

 protected function _prepareColumns()
    {      
         $this->addColumn('customer_id', array(
        'header'    => Mage::helper('sales')->__('Customer ID'),
        'width'     => '50px',
        'index'     => 'customer_id',
        'type'  => 'number',
    ));
   /*more columns*/
   return parent::_prepareColumns();
  }  }

我认为问题出在_prepareCollection()。此customer_id驻留在另一个表中,并且不存在于默认的Customer网格表中。请帮助,我尝试玩8小时但无法解决。

1 个答案:

答案 0 :(得分:0)

你没有提到你已经覆盖了Mage_Adminhtml_Block_Customer_Grid,因为你已经从core/Mage推出了代码。

但是如果你要覆盖它,那么我建议你不要覆盖_prepareCollection()而是覆盖方法setCollection()

public function setCollection($collection) {
    $collection->addAttributeToSelect('customer_id'); //although this may not add that column for you.
// you need to join the collection with your table in order to do that
    $this->_collection = $collection;
}

然后将您的列添加到网格中:

protected function _prepareColumns() {
    $this->addColumnAfter('customer_id', array(
        'header' => Mage::helper('customer')->__('Customer ID'),
        'type' => 'text',
        'index' => 'customer_id'
    ), 'entity_id');
    return parent::_prepareColumns();
}