0

I am using Magento 1.9.1.0 ce.

I have below code in my backend customer grid for getting collection of customers.

protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel('customer/customer_collection')
            ->addNameToSelect()
            ->addAttributeToSelect('email')
            ->addAttributeToSelect('created_at')
            ->addAttributeToSelect('group_id')

            ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
            ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
            ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
            ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
            ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');

            $collection->getSelect()->join( array('company' => 'customer_entity_varchar'), 'company.entity_id = e.entity_id', array('company.value'))
                 ->where("company.attribute_id =  982");

            ## echo $collection->getSelect();die;

         $this->setCollection($collection);

        return parent::_prepareCollection();
    }

It works fine when the grid is loaded the first time, but when I search by custom customer attribute called 'company' in the grid, it gives below fatal error:

Fatal error: Call to a member function getBackend() on a non-object in /path/to/magento_root/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 817

I think it gives fatal error when it doesn't get "company.value" field in the query. But the field is there in customer_entity_varchar table.

I want the grid to be able to search by custom customer attribute company.

2 Answers 2

1

Try this

In _prepareCollection() function:

protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel('customer/customer_collection')
            ->addNameToSelect()
            ->addAttributeToSelect('email')
            ->addAttributeToSelect('created_at')
            ->addAttributeToSelect('group_id')
            ->addAttributeToSelect('business_company')
            ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
            ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
            ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
            ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
            ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');


         $this->setCollection($collection);

        return parent::_prepareCollection();
    }

In _prepareColumns() function :

$this->addColumn('business_company', array(
            'header'    => Mage::helper('customer')->__('Company'),
            'width'     => '100',
            'index'     => 'business_company'
        ));
3
  • I want the search by company to work with customer grid, so please tell me how to make it work with company added in collection. Also I commented out the company field in the collection-query but still the same error persists.
    – Vicky Dev
    Commented Dec 28, 2015 at 6:16
  • is company customer attribute or address attribute?
    – Abdul
    Commented Dec 28, 2015 at 6:25
  • It's customer attribute, it's attribute_id is also there in customer_entity_varchar table. It breaks when it tries to find the value field of customer_entity_varchar although that field exists with value.
    – Vicky Dev
    Commented Dec 28, 2015 at 6:27
0

Got it working. Turns out it was issue with the column name passing in the grid columns.

Below is the code for adding custom customer attribute to grid with searching.

$this->addColumn('business_company', array(
            'header'    => Mage::helper('customer')->__('Company'),
            'width'     => '100',
            'index'     => 'business_company'
        ));

Turns out the field name I was passing was incorrect which was being sent to collection.

Your Answer

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

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