1

I have a module that has customer attributes added via the InstallSchema Script, however I have an UpgradeSchema script to also add more customer attributes;

Error below when running update in terminal;

PHP Fatal error:  Declaration of 
  MyCompany\Extendedcustomer\Setup\UpgradeSchema::upgrade
  (Magento\Framework\Setup\ModuleDataSetupInterface $setup,
   Magento\Framework\Setup\ModuleContextInterface $context) 

     must be compatible with 

Magento\Framework\Setup\UpgradeSchemaInterface::upgrade
 (Magento\Framework\Setup\SchemaSetupInterface $setup, 
  Magento\Framework\Setup\ModuleContextInterface $context) 

 in /app/code/MyCompany/Extendedcustomer/Setup/UpgradeSchema.php on line 17

Full Code:

<?php 

namespace MyCompany\Extendedcustomer\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeSchemaInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{


protected $customerSetupFactory;

public function upgrade(
    ModuleDataSetupInterface $setup, 
    ModuleContextInterface $context
) {
    $setup->startSetup();

    if (version_compare($context->getVersion(), '1.0.1') < 0) {

        $setup->startSetup();

        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerSetup->addAttribute(
                \Magento\Customer\Model\Customer::ENTITY,
                'current_device',                    
                [
                    'type' => 'text',
                    'label' => 'Current Device',
                    'input' => 'text',
                    'required' => false,
                    'visible' => true,
                    'user_defined' => true,
                    'sort_order' => 300,
                    'position' => 400,
                    'default' => '',
                    'system' => false,
                    'is_used_in_grid' => 0,
                    'is_visible_in_grid' => 1,
                    'is_filterable_in_grid' => 1,
                    'is_searchable_in_grid' => 1,
                ]
            );
        $Attribute = $customerSetup->getEavConfig()->getAttribute( \Magento\Customer\Model\Customer::ENTITY, 'current_device');
        $Attribute->setData(
        'used_in_forms',
            [
                'adminhtml_customer',
                'customer_account_edit',
                'adminhtml_checkout',
                'adminhtml_customer_address',
                'customer_address_edit',
                'customer_register_address',
            ]
        );
        $Attribute->save();

        $customerSetup->addAttribute(
                \Magento\Customer\Model\Customer::ENTITY,
                'current_scandevice',                    
                [
                    'type' => 'text',
                    'label' => 'Current RF Scan Device',
                    'input' => 'text',
                    'required' => false,
                    'visible' => true,
                    'user_defined' => true,
                    'sort_order' => 300,
                    'position' => 400,
                    'default' => '',
                    'system' => false,
                    'is_used_in_grid' => 0,
                    'is_visible_in_grid' => 1,
                    'is_filterable_in_grid' => 1,
                    'is_searchable_in_grid' => 1,
                ]
            );
        $Attribute = $customerSetup->getEavConfig()->getAttribute( \Magento\Customer\Model\Customer::ENTITY, 'current_scandevice');
        $Attribute->setData(
        'used_in_forms',
            [
                'adminhtml_customer',
                'customer_account_edit',
                'adminhtml_checkout',
                'adminhtml_customer_address',
                'customer_address_edit',
                'customer_register_address',
            ]
        );
        $Attribute->save();

        $setup->endSetup();

     }

   }
}
  • Refer to stackoverflow.com/a/35427023/1423608 – BENN1TH Nov 30 '18 at 9:08
  • You should try in UpgradeData, not in UpgradeSchema :) – Himmat Paliwal Nov 30 '18 at 9:15
  • As @HimmatPaliwal said, use UpgradeData instead of UpgradeSchema because adding a new attribute will insert a new entry of data in the eav_attributes table not adding or altering the database schema – Prince Patel Nov 30 '18 at 10:33
5

Please try with below code if it works:

<?php
namespace MyCompany\Extendedcustomer\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\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class UpgradeData implements UpgradeDataInterface
{
    protected $customerSetupFactory;

    private $attributeSetFactory;

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


    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();

        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
        if (version_compare($context->getVersion(), '1.0.1') < 0) {
            $customerSetup->addAttribute(
                    \Magento\Customer\Model\Customer::ENTITY,
                    'current_device',                    
                    [
                        'type' => 'text',
                        'label' => 'Current Device',
                        'input' => 'text',
                        'required' => false,
                        'visible' => true,
                        'user_defined' => true,
                        'sort_order' => 1000,
                        'position' => 1000,
                        'system' => 0,
                    ]
                );
            $Attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'current_device')
            ->addData([
                'attribute_set_id' => 1,
                'attribute_group_id' => 1,
                'used_in_forms' => ['adminhtml_customer','checkout_register', 'customer_account_create', 'customer_account_edit','adminhtml_checkout'],
            ]);

            $Attribute->save();

            $customerSetup->addAttribute(Customer::ENTITY, 'current_scandevice', [
                'type' => 'varchar',
                'label' => 'Current RF Scan Device',
                'input' => 'text',
                'required' => false,
                'visible' => true,
                'user_defined' => true,
                'sort_order' => 1000,
                'position' => 1000,
                'system' => 0,
            ]);

            $Attribute2 = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'current_scandevice')
            ->addData([
                'attribute_set_id' => 1,
                'attribute_group_id' => 1,
                'used_in_forms' => ['adminhtml_customer','checkout_register', 'customer_account_create', 'customer_account_edit','adminhtml_checkout'],
            ]);

            $Attribute2->save();

        }

        $setup->endSetup();
    }
}

Please let me know in case still it creates issue.

  • Thankyou @Himmat +1 – BENN1TH Nov 30 '18 at 9:21
  • Welcome anytime :) – Himmat Paliwal Nov 30 '18 at 9:44
0

DO it by InstallData. Way I prefere app/code/vendor/moduel/Setup/InstallData.php

    <?php

namespace Vendor/Moduel\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Config;
use Magento\Customer\Model\Customer;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig)
    {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig       = $eavConfig;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'sample_attribute',
            [
                'type'         => 'varchar',
                'label'        => 'Sample Attribute',
                'input'        => 'text',
                'required'     => false,
                'visible'      => true,
                'user_defined' => true,
                'position'     => 999,
                'system'       => 0,
            ]
        );
        $sampleAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'sample_attribute');

        // more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
        $sampleAttribute->setData(
            'used_in_forms',
            ['adminhtml_customer']

        );
        $sampleAttribute->save();
    }
}
  • I have already run InstallSchema, module is in upgrading level – BENN1TH Nov 30 '18 at 9:18
  • What happens when I have already run InstallData but have new attributes to add via version changes .. – BENN1TH Nov 30 '18 at 9:19

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.