Hi, i have question about add new customer fields. I try to add two fields like this:
MyModules\CustomerMarketingFields\Setup\InstallData.php
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->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "regulation", array(
"type" => "int",
"backend" => "",
"label" => "Regulamin",
"input" => "boolean",
"source" => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
"visible" => true,
"required" => true,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => ""
));
$regulation = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "regulation");
$regulation = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'regulation');
$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";
$regulation->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);
$regulation->save();
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "process_personal_data", array(
"type" => "int",
"backend" => "",
"label" => "Personal data",
"input" => "boolean",
"source" => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
"visible" => true,
"required" => true,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => ""
));
$personalData = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "process_personal_data");
$personalData = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'process_personal_data');
$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";
$personalData->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);
$personalData->save();
$installer->endSetup();
}
After run setup:upgrade command i get error like this:
[Zend_Db_Statement_Exception]
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'adminhtml_customer-147' for key 'PRIMARY', query was: INSERT INTO `customer_form_attribute` (`form_code`,`attribute_id`) VALUES (?, ?), (?, ?), (?, ?), (?, ?), (?, ?), (?, ?), (?, ?)
[PDOException]
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'adminhtml_customer-147' for key 'PRIMARY'
When I add only one field everything is ok.
What is wrong with this config?