2

I added a column in customer_grid_flat table using UpgradeSchema.php . But each time I run the command php bin/magento indexer:reindex this column gets deleted. I cannot figure out why this happen.Please help in this regard. I attached below the customer_eav_attribute and eav_attribute. enter image description here

enter image description here

  • What column you have added? Any customer attribute? – Sukumar Gorai Sep 12 '18 at 7:31
  • I added a column named customer_type – Magento Learner Sep 12 '18 at 7:36
  • Check my answer. – Sukumar Gorai Sep 12 '18 at 7:48
1

Can you try using InstallData instead of Upgrade schema -

<?php 

namespace Namespace\Modulename\Setup; 

use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{

    /**
     * @var \Magento\Customer\Setup\CustomerSetupFactory
     */
    private $customerSetupFactory;

    /**
     * @var \Magento\Eav\Model\Entity\Attribute\SetFactor
     */
    private $attributeSetFactory;    

    /**
     * Init
     *
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }    

    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_type', [
            'type' => 'int',
            'label' => 'Customer Type',
            'input' => 'select',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 100,
            'position' => 100,
            'is_used_in_grid' => true,
            'system' => 0,
            'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
            'adminhtml_only'=>1,
            'default'=>0
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'customer_type')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer'],
        ]);

        $attribute->save();
    }
}

Then in your module in customer_columns tag in customer_listing.xml :

<column name="customer_type">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">select</item>
                    <item name="editor" xsi:type="string">select</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
                    <item name="dataType" xsi:type="string">select</item>
                    <item name="label" xsi:type="string" translate="true">Customer Type</item>
                    <item name="sortOrder" xsi:type="number">100</item>
                </item>
            </argument>
        </column>

Then create app/etc/indexer.xml :

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Indexer/etc/indexer.xsd">
    <indexer id="customer_grid">
        <fieldset name="customer">
            <field name="customer_type" xsi:type="filterable" dataType="int"/>
        </fieldset>
    </indexer>
</config>
1

You need to use is_used_in_grid as true while creating your customer attribute like below.

$customerSetup->addAttribute(
    Customer::ENTITY,
        'customer_type',
        [
            'type' =>'varchar',
            'label' => 'Customer Type',
            'input' => 'text',
            'required' => false,
            'sort_order' => 101,
            'visiable' => true,
            'position' => 101,
            'system' =>  false,
            'unique' => true,
            'is_used_in_grid' => true,
            'is_visible_in_grid' => false,
            'is_filterable_in_grid' => false,
            'is_searchable_in_grid' => false,
        ]
    );

OR

If you have already created this attribute and need to change the value then you can follow the below steps:

Step 1:

Goto eav_attribute table and search your attribute code customer_type in the column attribute_code. You will get the attribute_id.

Step 2:

Goto customer_eav_attribute table and search the row with attribute_id which you get from step 1.

Step 3:

Now update the columns is_used_in_grid , is_visible_in_grid , is_filterable_in_grid and is_searchable_in_grid to 1.

Step 4:

Flush the cache and run reindex and check.

  • It is already set as you said in db. – Magento Learner Sep 12 '18 at 8:47
  • Can you take screenshot of the table and update to your question? – Sukumar Gorai Sep 12 '18 at 8:48

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.