Reading list Switch to dark mode

    Invalid attribute error in admin panel grid

    Updated 9 August 2018

    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 –

    Searching for an experienced
    Magento Company ?
    Find out More

     

    $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 🙂

     

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home