这其实一点都不难!
当我这样做时,我将 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);
}
希望这可以帮助某人。