I have Magento 2.1.3 and have added a custom field in to the admin area of customers which works nicely, however I am now trying to add that field to the customer grid view in the admin area so we can search and filter by that field.
I have got the column to appear in the customer grid view but the data values for that field/record are not showing, the column is just blank.
'I think' I have to add to indexer.xml, but when I do a reindex I get an SQL error - details below...
Here is my Setup/InstallData.php
<?php
namespace custom\CustomerPut\Setup;
use Magento\Framework\Module\Setup\Migration;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
/**
* Customer setup factory
*
* @var \Magento\Customer\Setup\CustomerSetupFactory
*/
private $customerSetupFactory;
/**
* Init
*
* @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
*/
public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
{
$this->customerSetupFactory = $customerSetupFactory;
}
/**
* Installs DB schema for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$entityTypeId = $customerSetup->getEntityTypeId(\Magento\Customer\Model\Customer::ENTITY);
$customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, "customer_account");
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "customer_account", array(
"type" => "varchar",
"backend" => "",
"label" => "Customer Account",
"input" => "text",
"source" => "",
"visible" => true,
"required" => true,
"default" => "",
"frontend" => "",
"is_used_in_grid" => true,
"unique" => false,
"note" => ""
));
$customer_account = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "customer_account");
$customer_account = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'customer_account');
$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$used_in_forms[]="adminhtml_checkout";
$customer_account->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 100);
$customer_account->save();
$installer->endSetup();
}
}
Here is my 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" class="Magento\Framework\Indexer\Action\Entity" primary="customer">
<fieldset name="customer" source="Magento\Customer\Model\ResourceModel\Customer\Collection" provider="Magento\Customer\Model\Indexer\AttributeProvider">
<field name="customer_account" xsi:type="filterable" dataType="varchar"/>
</fieldset>
</indexer>
</config>
When I rebuild in the index I get the following SQL error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'e.customer_account'
What am I missing? Any help would be most appreciated Thanks