Reputation: 2949
I have 2 custom colletions. Usual collections with flat data. I need to join them to customers select. It works fine with innerJoin, but filtering and sorting for joined fields doesn't work. How can I solve this?
_prepareCollection() example
$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();
So, sorting and filtering will not work for custom_field and custom_field1
Add column call:
$this->addColumn('custom_field',
array(
'header'=>$this->__('Shopping club name'),
'index'=>'custom_field',
'filter_index'=>'my_table.custom_field',
));
While filtering I get fatal error:
Call to a member function getBackend() on a non-object
Sorting doesn't work, no errors shown
'filter_index' works fine if you join flat to flat tables. But here flat is joined to EAV.
Upvotes: 3
Views: 11731
Reputation: 1761
This isn't too hard at all actually!
When I did this I was adding the last_login to the customer grid.
The first step, which you have already done but I'm including here for completeness, is to add the column to the initial select statement:
/**
* 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);
}
Next, we add the column to the grid. Notice we are defining two callbacks. 'filter_condition_callback' is stock Magento. Oddly, there is no callback index (so far as I am aware) to sort on this. We need to add a sort callback otherwise we can't sort our new column.
$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');
Next, we add the callback functions that will handle the filtering & sorting:
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()));
}
}
Finally, since the order_callback is not a real index we need to call this callback if defined instead of the default sorting mechanism. This is how i accomplished it:
/**
* 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);
}
Hope this helps someone.
Upvotes: 15
Reputation: 1895
Could you show a call of addColumn() that adds your attributes to grid?
It should be like:
$this->addColumn('custom_field', array(
...
'filter_index' => 'my_table.custom_field',
...
));
Probably you set the alias value for the 'filter_index' key.
Upvotes: 6