Menu Close
    Hire Us Request Quote Reading List Switch to Dark Mode

    Invalid attribute error in admin panel grid

    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 🙂

     

    . . .

    Comment

    Add Your Comment

    Be the first to comment.

    Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close