我有2个自定义收藏.平面数据的常用集合.我需要加入他们给客户选择.它适用于innerJoin,但对连接字段的过滤和排序不起作用.我怎么解决这个问题?
_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();
Run Code Online (Sandbox Code Playgroud)
因此,排序和过滤不适用于custom_field和custom_field1
添加列调用:
$this->addColumn('custom_field',
array(
'header'=>$this->__('Shopping club name'),
'index'=>'custom_field',
'filter_index'=>'my_table.custom_field',
));
Run Code Online (Sandbox Code Playgroud)
过滤时我得到了致命的错误:
Call to a member function getBackend() on a non-object
Run Code Online (Sandbox Code Playgroud)
排序不起作用,没有显示错误
如果您将平面连接到平面表,'filter_index'可以正常工作.但这里平坦加入了EAV.
Ian*_*Ian 15
这实际上并不太难!
当我这样做时,我将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);
}
Run Code Online (Sandbox Code Playgroud)
接下来,我们将列添加到网格中.请注意,我们正在定义两个回调.'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');
Run Code Online (Sandbox Code Playgroud)
接下来,我们添加将处理过滤和排序的回调函数:
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()));
}
}
Run Code Online (Sandbox Code Playgroud)
最后,由于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);
}
Run Code Online (Sandbox Code Playgroud)
希望这有助于某人.
你能展示一个addColumn()调用,它将你的属性添加到网格中吗?
应该是这样的:
$this->addColumn('custom_field', array(
...
'filter_index' => 'my_table.custom_field',
...
));
Run Code Online (Sandbox Code Playgroud)
您可能为"filter_index"键设置了别名值.
| 归档时间: |
|
| 查看次数: |
10771 次 |
| 最近记录: |