1

I have question about add new customer fields. Trying 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 'admi  
  nhtml_customer-147' for key 'PRIMARY'

When I add only one field everything is ok. What is wrong with this config?

1

You are setting twice $used_in_forms. So in $personalData->setData() you pass:

$used_in_forms = [
   ...
   "adminhtml_customer"
   "adminhtml_customer"
   ...
]
0

Your install class name should be InstallSchema (setup/InstallSchema.php).

& many other changes needed Hope this link will help you.

Magento 2 Database upgrade

  • File is properly run and when I add only one field everything is ok, so I think that the problem is somewhere else. Schema is changed when I want for example add column to existing database I think. – Mateusz_Procner Apr 26 '16 at 10:56
0

I have tried the same code and same error showing on my screen. Than I have checked the "customer_form_attribute" table into database and found that it has to columns "form_code" and "attribute_id", both are setup as a primary keys. I have just remove the primary key from the "form_code" column and run the script again by deleting the our module entry from "setup_module" table.

And now all working for me and created two customer attribute "Regulamin" and "Personal data" into admin customer add/edit section.

I have changed into table structure manually.I don't know its best practice or not but its working now.::)

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.