2

I tried to run my integration test using

bin/magento dev:tests:run integration

but i always got an error like this:

Module 'Company_CutomerExtraAttribute':
Upgrading data...

[Magento\Framework\Exception\LocalizedException]
Invalid entity_type specified: customer

Exception trace:
() at /var/www/project/app/code/Magento/Eav/Model/Config.php:409
Magento\Eav\Model\Config->getEntityType() at /var/www/project/app/code/Company/CutomerExtraAttribute/Setup/UpgradeData.php:39

UpgradeData.php

<?php

namespace Company\CutomerExtraAttribute\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\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;

class UpgradeData implements  UpgradeDataInterface
{
    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }

    public function upgrade(ModuleDataSetupInterface $setup,
                            ModuleContextInterface $context){
        $setup->startSetup();
        if (version_compare($context->getVersion(), '1.0.2') < 0) {
          $insertArray = array(
            'id_card' => array("type" => "varchar", "label" => "Telephone"),
            'security_question_1' => array("type" => "int", "label" => "Security Question 1"),
            'security_question_2' => array("type" => "int", "label" => "Security Question 2"),
            'security_answer_1' => array("type" => "varchar", "label" => "Security Answer 1"),
            'security_answer_2' => array("type" => "varchar", "label" => "Security Answer 2")
          );

          foreach ($insertArray as $key => $data) {
            $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, $key, [
                'type' => $data['type'],
                'label' => $data['label'],
                'input' => 'text',
                'required' => false,
                'visible' => true,
                'user_defined' => true,
                'sort_order' => 1000,
                'position' => 1000,
                'system' => 0,
            ]);

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

            $attribute->save();
          }

        }
   }
}

I never have a conflict with this UpgradeData.php when i ran setup:upgrade, and the customer eav installed properly after i ran setup:upgrade

Your Answer

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

Browse other questions tagged or ask your own question.