Sometimes you might face an error “Invalid Attribute” in admin panel grid during filtering or sorting after you have joined product (for example) with your table.
Suppose your join is something like this-
protected function _prepareCollection() { $collection = Mage::getModel('catalog/product')->getCollection(); $prefix = Mage::getConfig()->getTablePrefix(); $collection->getSelect()->joinLeft( array('map' => $prefix."webkul_product_demo_mapping"), 'map.magento_id = e.entity_id', array('magento_id','status','created_at') ); $this->setCollection($collection); return parent::_prepareCollection(); }
You are displaying your grid column like this
$this->addColumn('magento_id', array( 'header' => Mage::helper('webkul')->__('Magento Id'), 'align' => 'center', 'index' => 'magento_id', ));
Now if you are getting “Invalid Attribute” error during searching or sorting your column magento id, it is because magento thinks magento_id you are calling an attribute of product but as magento_id is not an attribute of product so it displays an error.
So what you need to do is use the following code –
$this->addColumn('magento_id', array( 'header' => Mage::helper('webkul')->__('Magento Id'), 'align' => 'center', 'index' => 'magento_id', 'filter_condition_callback' => array($this, 'filterMagentoId'), 'order_callback' => array($this, 'orderMagentoId') ));
Here two functions are called.
For filter
protected function filterMagentoId($collection, $column) { if ($column->getFilter()->getValue() === null) { return; } $collection->getSelect()->where('map.magento_id like ?', '%' . $column->getFilter()->getValue() . '%'); }
For sorting
protected function orderMagentoId($collection, $column) { $collection->getSelect()->order($column->getIndex() . ' ' . strtoupper($column->getDir())); }
That’s it.
Happy coding 🙂
Be the first to comment.