2

I followed this article http://www.extensions.sashas.org/blog/magento-2-1-3-how-to-make-customer-attribute-update.html to add 3 attributes this is My Install.php

<?php

namespace Magenticians\CustomerAttribute\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;

/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{

    /**
     * @var CustomerSetupFactory
     */
    protected $customerSetupFactory;

    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;

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


    /**
     * {@inheritdoc}
     */
    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, 'birth', [
            'type' => 'static',
            'label' => 'Birth',
            'input' => 'date',
            'required' => true,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 1000,
            'position' => 1000,
            'system' => 0,
        ]);

        $customerSetup->addAttribute(Customer::ENTITY, 'adresse', [
            'type' => 'varchar',
            'label' => 'Adresse',
            'input' => 'text',
            'required' => true,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 1001,
            'position' => 1001,
            'system' => 0,
        ]);

        $customerSetup->addAttribute(Customer::ENTITY, 'phone', [
            'type' => 'varchar',
            'label' => 'Phone Number',
            'input' => 'text',
            'required' => true,
            'visible' => true,
            'user_defined' => true,
            'validate_rules' => '{"max_text_length":255,"min_text_length":1}',
            'sort_order' => 1002,
            'position' => 1002,
            'system' => 0,
        ]);



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

        $secondattribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'adresse')
            ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => ['customer_account_create'],
            ]);


        $thirdattribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'phone')
            ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => ['customer_account_create'],
            ]);

        $attribute->save();
        $secondattribute->save();
        $thirdattribute->save();



    }
}

I also added to register.phtml

 <div class="field required">
            <label for="adresse" class="label"><span><?php /* @escapeNotVerified */ echo __('Adresse') ?></span></label>
            <div class="control">
                <input type="text" name="adresse" id="adresse" value="<?php echo $block->escapeHtml($block->getFormData()->getAdresse()) ?>" title="
                <?php /* @escapeNotVerified */ echo __('Adresse') ?>" class="input-text">
            </div>
        </div>

        <div class="field required">
            <label for="birth" class="label"><span><?php /* @escapeNotVerified */ echo __('Birth') ?></span></label>
            <div class="control">
                <input type="date" name="birth" id="birth" value="<?php echo $block->escapeHtml($block->getFormData()->getBirth()) ?>" title="
                <?php /* @escapeNotVerified */ echo __('Birth') ?>" class="input-text">
            </div>
        </div>

        <div class="field required">
            <label for="phone" class="label"><span><?php /* @escapeNotVerified */ echo __('Phone Number') ?></span></label>
            <div class="control">
                <input type="phone" name="phone" id="phone" value="<?php echo $block->escapeHtml($block->getFormData()->getPhone()) ?>" title="
                <?php /* @escapeNotVerified */ echo __('Phone Number') ?>" class="input-text" pattern='[\+]\d{2}[\(]\d{2}[\)]\d{4}[\-]\d{4}' >
            </div>
        </div>

I got this error when I tried to add new customer:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'sajjada_site.customer_entity_date' doesn't exist, query was: DESCRIBE `customer_entity_date`

please help me figure out the problem and thanks in advance.

1 Answer 1

2

You did a mistake.

For attribute birth, the type should be

datetime'type' => 'datetime'

instead of

date 'type' => 'date'

If you have checked the error

 'sajjada_site.customer_entity_date' doesn't exist, query was: DESCRIBE `customer_entity_date`

then you have found that customer_entity_date table does not exist at the database for a Magento eav module.

Usually, Magento use customer_entity_datetime for date save types attribute value for customer entity.

2
  • I remplaced date by datetime and i just run upgrade, indexer:reindex flush but same error Commented Aug 23, 2018 at 9:19
  • i just removed the module and increased module 1.0.3 and it works well Commented Aug 23, 2018 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.