1

I have created custom customer attribute. It is displaying in customer grid in admin panel but when I try to filter on my custom attribute, I get error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'customer_attribute' in 'where clause'.

This is because it's not available in table customer_grid_flat I have already done re-indexing.

When I change value of is_used_in_grid to 1 from table customer_eav_attribute and start re indexing i am getting following error

PHP Fatal error: Uncaught Error: Call to undefined method Magento\Customer\Model\Indexer\Source::addAttributeToSelect() in /opt/lampp/htdocs/magento-221/vendor/magento/framework/Indexer/Handler/AttributeHandler.php:38

I have tried changing following column value to 1 but not working.

is_visible_in_grid
is_filterable_in_grid
is_searchable_in_grid

Here is setup file code

use Magento\Eav\Model\Config;
use Magento\Eav\Model\Entity\Setup\Context;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;

class CustomerSetup extends EavSetup {

    protected $eavConfig;

    public function __construct(
        ModuleDataSetupInterface $setup,
        Context $context,
        CacheInterface $cache,
        CollectionFactory $attrGroupCollectionFactory,
        Config $eavConfig
        ) {
        $this -> eavConfig = $eavConfig;
        parent :: __construct($setup, $context, $cache, $attrGroupCollectionFactory);
    } 

    public function installAttributes($customerSetup) {
        $this -> installCustomerAttributes($customerSetup);
        $this -> installCustomerAddressAttributes($customerSetup);
    } 

    public function installCustomerAttributes($customerSetup) {


        $customerSetup -> addAttribute(\Magento\Customer\Model\Customer::ENTITY,
            'customer_approval',
            [
            'label' => 'Customer Approve',
            'system' => 0,
            'position' => 108,
            'sort_order' =>108,
            'visible' =>  false,
            'note' => 'Customer Approval',


                        'type' => 'int',
                        'input' => 'boolean',
                        'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',

            ]
            );

        $customerSetup -> getEavConfig() -> getAttribute('customer', 'customer_approval')->setData('is_user_defined',1)->setData('is_required',0)->setData('default_value','')->setData('used_in_forms', ['adminhtml_customer', 'customer_account_create', 'customer_account_edit']) -> save();


    } 

    public function installCustomerAddressAttributes($customerSetup) {

    } 

    public function getEavConfig() {
        return $this -> eavConfig;
    } 

Can anyone please let me know what is wrong with that.

  • 1
    Use addFieldToSelect instead of addAttributeToSelect. addFieldToSelect used for flat model addAttributeToSelect user for EAV model – fmsthird Mar 14 '19 at 9:44
  • @magefms I am not using any custom filter coding. When customer attribute create, it will display under filter option in admin – user55548 Mar 14 '19 at 9:46
  • can you share your updradedata.php or installdata.php files, – Anand Ontigeri Mar 14 '19 at 10:28
  • @AnandOntigeri I have update code in my question. – user55548 Mar 14 '19 at 10:51
  • I have answered your question, I think the problem with options and is_used_in_grid , if you want to see this filed in grid is_used_in_grid should be yes "is_used_in_grid =1) then only you can apply filter in the grid. – Anand Ontigeri Mar 14 '19 at 11:11
0

First, you need to delete the attribute from the system and try the below code:

I think the problem with options and is_used_in_grid if you want to see this field in the grid is_used_in_grid should be yes (is_used_in_grid =1) then only you can apply the filter in the grid.

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context){
        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();
        /** @var $attributeSet AttributeSet */
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
        $customerSetup->addAttribute(Customer::ENTITY, 'customer_approval', [
            'type'           => 'int',
            'label'          => 'customer approval',
            'input'          => 'bool',
            'option'         => ['values' => ['0', '1']],
            'source'         => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
            'required'       => 0,
            'sort_order'     => 210,
            'visible'        => 0,
            'position'       => 300,
            'admin_checkout' => 1,
            'system'         => 0,
            'is_null'        => false,
            'user_defined'   => true,
            'is_system'      => 0,
            'is_used_in_grid'              => 1,
            'is_visible_in_grid'           => 1,
            'is_filterable_in_grid'        => 1,
            'is_searchable_in_grid'        => 1,
            'is_used_for_customer_segment' => 1,
        ]);
        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'customer_approval');
        $attribute->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer', 'customer_account_create', 'customer_form_attribute'],
        ]);
        $attribute->save();
}
  • I have add above code in installData.php and run setup upgrade command. I am getting error Notice: Undefined property: Vendor\module name\Setup\InstallData::$customerSetupFactory in /opt/lampp/htdocs/magento-221/app/code/MES/CustomerApproval/Setup/InstallData.php on line 48 – user55548 Mar 14 '19 at 11:21
  • On line 48 $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); code is writtent – user55548 Mar 14 '19 at 11:21
  • in constructor did you mentioned Magento\Customer\Setup\CustomerSetupFactory; – Anand Ontigeri Mar 14 '19 at 11:22
  • public function __construct( CustomerSetupFactory $customerSetupFactory, AttributeSetFactory $attributeSetFactory ) { $this->customerSetupFactory = $customerSetupFactory; $this->attributeSetFactory = $attributeSetFactory; } – Anand Ontigeri Mar 14 '19 at 11:23
  • After update constructor code now, i get error "We can't find the role for the user you wanted." – user55548 Mar 14 '19 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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