2013-01-16 23 views
2

我有2個自定義收集。具有平坦數據的常用集合。 我需要加入他們的客戶選擇。它適用於innerJoin,但對連接字段進行過濾和排序不起作用。我該如何解決這個問題?Magento Grid - 在將平臺加入EAV後排序和過濾工作不正確

_prepareCollection()例如

$collection = Mage::getResourceModel('customer/customer_collection') 
    ->addNameToSelect() 
    ->addAttributeToSelect('email'); 

$collection 
    ->getSelect() 
    ->joinInner(array('my_table' => $collection->getTable('my/table')), 'e.entity_id = my_table.customer_id', array('custom_field' => my_table.custom_field)) 
    ->joinInner(array('my_table1' => $collection->getTable('my/table1')), 'my_table1.other_id = my_table.id', array('custom_field1' => my_table1.custom_field)); 

$this->setCollection($collection); 
return parent::_prepareCollection(); 

因此,排序和過濾不會爲custom_field工作,custom_field1

添加列呼叫:

$this->addColumn('custom_field', 
     array(
      'header'=>$this->__('Shopping club name'), 
      'index'=>'custom_field', 
      'filter_index'=>'my_table.custom_field', 
    )); 

同時過濾我得到致命錯誤:

Call to a member function getBackend() on a non-object 

排序不起作用,沒有顯示錯誤

'filter_index'可以正常工作,如果你加入扁平表。但這裏平坦的加入了EAV。

回答

14

這實際上並不難!

當我這樣做時,我將last_login添加到客戶網格中。

的第一步,你已經做了,但我這裏包括的完整性,是對列添加到初始SELECT語句:

/** 
* set collection object 
* 
* @param Mage_Customer_Model_Resource_Customer_Collection $collection 
*/ 
public function setCollection($collection) 
{ 
    //Group by email since multiple customer_ids can exist in the log/customer. 
    $collection->groupByEmail(); 

    //join the last_login field here. 
    $collection->getSelect()->joinLeft(array('c_log' => $collection->getTable('log/customer')), 'c_log.customer_id=e.entity_id', array('last_login' => 'login_at')); 

    parent::setCollection($collection); 
} 

接下來,我們將列添加到網格。注意我們正在定義兩個回調。 'filter_condition_callback'是股票Magento。奇怪的是,沒有回調指數(據我所知)來排序。我們需要添加一個排序回調,否則我們不能排序我們的新列。

$this->addColumnAfter('last_login', array(
     'header'     => Mage::helper('customer')->__('Last Login'), 
     'type'      => 'datetime', 
     'align'      => 'center', 
     'index'      => 'last_login', 
     'filter_index'    => '`c_log`.`login_at`', 
     'gmtoffset'     => true, 
     //Stock Magento Callback - Notice the callback key has been assigned. 
     'filter_condition_callback' => 'filter_last_login', 
     //Custom Callback Index 
     'order_callback'   => 'sort_last_login' 
    ), 'customer_since'); 

接下來,我們添加回調函數將處理過濾&排序:

if (!function_exists('filter_last_login')) { 
    /** 
    * @param Mage_Customer_Model_Resource_Customer_Collection $collection 
    * @param Mage_Adminhtml_Block_Widget_Grid_Column   $column 
    */ 
    function filter_last_login($collection, $column) 
    { 
     if (!$column->getFilter()->getCondition()) { 
      return; 
     } 

     $condition = $collection->getConnection() 
      ->prepareSqlCondition('c_log.login_at', $column->getFilter()->getCondition()); 
     $collection->getSelect()->where($condition); 
    } 
} 

if (!function_exists('sort_last_login')) { 
    /** 
    * @param Mage_Customer_Model_Resource_Customer_Collection $collection 
    * @param Mage_Adminhtml_Block_Widget_Grid_Column   $column 
    */ 
    function sort_last_login($collection, $column) 
    { 
     $collection->getSelect()->order($column->getIndex() . ' ' . strtoupper($column->getDir())); 
    } 
} 

最後,由於order_callback是不是我們需要調用這個回調函數,如果定義的,而不是一個真正的指數默認排序機制。這是我的成就:

/** 
* Sets sorting order by some column 
* 
* @param Mage_Adminhtml_Block_Widget_Grid_Column $column 
* 
* @return Mage_Adminhtml_Block_Widget_Grid 
*/ 
protected function _setCollectionOrder($column) 
{ 
    if ($column->getOrderCallback()) { 
     call_user_func($column->getOrderCallback(), $this->getCollection(), $column); 

     return $this; 
    } 

    return parent::_setCollectionOrder($column); 
} 

希望這可以幫助別人。

+0

出色地工作!排序真棒!謝謝! – freento

+2

可能需要一些更改: 'filter_condition_callback'=>數組($ this,'filter_last_login'), 'order_callback'=> array($ this,'sort_last_login') –

+0

這個問題很老,所以我不打算更新。但是,黃,添加更好。那時我不知道這件事。可以定義集合內的功能,好多了。 – Ian

1

我寫了一篇關於這方面的文章。看看這裏: http://blog.fabian-blechschmidt.de/joining-a-flat-table-on-eav/ 希望它有幫助。

+0

沒問題加入平面收藏。問題是使用連接的字段進行排序和過濾工作。 – freento

+0

你的意思是,你想要網格中的這些字段單擊標題? –

+0

是的 - 這是排序。另外我需要過濾 – freento

5

您可以顯示addColumn()的調用,它將您的屬性添加到網格嗎?

應該是這樣:

$this->addColumn('custom_field', array(
    ... 
    'filter_index' => 'my_table.custom_field', 
    ... 
)); 

也許你設置爲 'filter_index' 鍵的別名值。

+0

編輯問題。 – freento

+0

如果您將平面連接到平面表,filter_index可以正常工作。但這裏平坦的加入了EAV。 – freento