3

I have a custom grid with columns created by joining a flat table to the customer collection. Everything works fine except when i try to filter the list using any of the column added from the flat table.

In _prepareCollection()

    $collection = Mage::getModel('customer/customer')->getCollection()
            ->addNameToSelect()
            ->addAttributeToSelect('email')
            ->addAttributeToSelect('created_at')
            ->addAttributeToSelect('group_id')
            ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');

    $collection->joinTable(array('up' => 'mymodule/userprofile'), 'mageuserid=entity_id', array('*'), null, 'left');
    $collection->addAttributeToFilter('entity_id', array('in' => $record));
    $collection->addFilterToMap("shoptitle", "up.shoptitle");
    $collection->addFilterToMap("contactnumber", "up.contactnumber");
    $this->setCollection($collection);
    parent::_prepareCollection();

Added columns in _prepareColumns()

    $this->addColumn('shoptitle', array(
        'header' => Mage::helper('mymodule')->__('Shop Title'),
        'index' => 'shoptitle',
        'type' => 'text',
    ));
    $this->addColumn('contactnumber', array(
        'header' => Mage::helper('mymodule')->__('Telephone'),
        'width' => '100',
        'index' => 'contactnumber',
    ));

Here is my error log

    exception 'Exception' with message 'Non Object found for attribute shoptitle' in C:\xampp\htdocs\mage_test\app\code\core\Mage\Eav\Model\Entity\Abstract.php:785
    Stack trace:
    #0 C:\xampp\htdocs\mage_test\app\code\core\Mage\Eav\Model\Entity\Collection\Abstract.php(1377): Mage_Eav_Model_Entity_Abstract->isAttributeStatic('shoptitle')
    #1 C:\xampp\htdocs\mage_test\app\code\core\Mage\Eav\Model\Entity\Collection\Abstract.php(321): Mage_Eav_Model_Entity_Collection_Abstract->_getAttributeConditionSql('shoptitle', Array, 'inner')
    #2 C:\xampp\htdocs\mage_test\app\code\core\Mage\Eav\Model\Entity\Collection\Abstract.php(341): Mage_Eav_Model_Entity_Collection_Abstract->addAttributeToFilter('shoptitle', Array)
    #3 C:\xampp\htdocs\mage_test\app\code\core\Mage\Adminhtml\Block\Widget\Grid.php(472): Mage_Eav_Model_Entity_Collection_Abstract->addFieldToFilter('shoptitle', Array)
    #4 C:\xampp\htdocs\mage_test\app\code\core\Mage\Adminhtml\Block\Widget\Grid.php(457): Mage_Adminhtml_Block_Widget_Grid->_addColumnFilterToCollection(Object(Mage_Adminhtml_Block_Widget_Grid_Column))
    #5 C:\xampp\htdocs\mage_test\app\code\core\Mage\Adminhtml\Block\Widget\Grid.php(517): Mage_Adminhtml_Block_Widget_Grid->_setFilterValues(Array)
    #6 C:\xampp\htdocs\mage_test\app\code\local\Test\Mymodule\Block\Adminhtml\Partners\Grid.php(52): Mage_Adminhtml_Block_Widget_Grid->_prepareCollection()
    #7 C:\xampp\htdocs\mage_test\app\code\core\Mage\Adminhtml\Block\Widget\Grid.php(626): Test_Mymodule_Block_Adminhtml_Partners_Grid->_prepareCollection()
    #8 C:\xampp\htdocs\mage_test\app\code\core\Mage\Adminhtml\Block\Widget\Grid.php(632): Mage_Adminhtml_Block_Widget_Grid->_prepareGrid()
    #9 C:\xampp\htdocs\mage_test\app\code\core\Mage\Core\Block\Abstract.php(918): Mage_Adminhtml_Block_Widget_Grid->_beforeToHtml()
    #10 C:\xampp\htdocs\mage_test\app\code\local\Test\Mymodule\controllers\Adminhtml\Mymodule\PartnersController.php(56): Mage_Core_Block_Abstract->toHtml()
    #11 C:\xampp\htdocs\mage_test\app\code\core\Mage\Core\Controller\Varien\Action.php(418): Test_Mymodule_Adminhtml_Mymodule_PartnersController->gridAction()
    #12 C:\xampp\htdocs\mage_test\app\code\core\Mage\Core\Controller\Varien\Router\Standard.php(254): Mage_Core_Controller_Varien_Action->dispatch('grid')
    #13 C:\xampp\htdocs\mage_test\app\code\core\Mage\Core\Controller\Varien\Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
    #14 C:\xampp\htdocs\mage_test\app\code\core\Mage\Core\Model\App.php(365): Mage_Core_Controller_Varien_Front->dispatch()
    #15 C:\xampp\htdocs\mage_test\app\Mage.php(684): Mage_Core_Model_App->run(Array)
    #16 C:\xampp\htdocs\mage_test\index.php(83): Mage::run('', 'store')
    #17 {main}
1
  • Thanks for the answer rafal-kos helped a lot for me to solve a big client issue.
    – Raghu
    May 17, 2018 at 7:32

1 Answer 1

5

Why not use something like that :

$collection->joinTable(array('mup' => 'marketplace/userprofile'), 'mageuserid=entity_id', array('*'), null, 'left');

and then add filter callback on specific columns :

$this->addColumn('shoptitle', array(
            'header'    => Mage::helper('customer')->__('Shop Title'),
            'index'     => 'shoptitle',
            'filter_condition_callback' => array($this, '_filterShopTitle'),
            'order_callback'            => array($this, '_sortShopTitle')
        ));

_filterShopTitle function body

    /**
     * Function that handle filter on custom table
     *
     * @param $collection
     * @param $column
     */
    protected function _filterShopTitle($collection, $column)
    {
        if ($column->getFilter()->getValue() === null) {
            return;
        }

        $collection->getSelect()->where('mup.shoptitle like ?', '%' . $column->getFilter()->getValue() . '%');
    }

and for sorting :

    /**
     * Function that handle sorting on custom table
     *
     * @param Mage_Customer_Model_Resource_Customer_Collection $collection
     * @param Mage_Adminhtml_Block_Widget_Grid_Column          $column
     */
    function _sortShopTitle($collection, $column)
    {
        $collection->getSelect()->order($column->getIndex() . ' ' . strtoupper($column->getDir()));
    }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.