cancel
Showing results for 
Search instead for 
Did you mean: 

[Magento2.2]Creating a custom Customer attribute through module upgrade (UpgradeData.php)

SOLVED

[Magento2.2]Creating a custom Customer attribute through module upgrade (UpgradeData.php)

As title says, i'm having troubles at creating new customers attribute when upgrading one of my modules. 

I've created the UpgradeData.php file under /app/code/vendor/modulename/Setup/UpgradeData.php with the current code:

 

<?php


namespace Ucs\CustomerAttribute\Setup;

use Magento\Customer\Model\Customer;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Customer\Setup\CustomerSetupFactory;

class UpgradeData implements UpgradeDataInterface
{

    private $customerSetupFactory;

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

    /**
     * {@inheritdoc}
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context){

        $setup->startSetup();

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

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

            $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'nome_azienda', [
                'type' => 'varchar',
                'label' => 'Nome azienda',
                'input' => 'text',
                'source' => '',
                'required' => false,
                'visible' => true,
                'position' => 333,
                'system' => false,
                'backend' => ''
            ]);

            $attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'nome_azienda')
                ->addData(['used_in_forms' => [
                    'adminhtml_customer',
                    'adminhtml_checkout',
                    'customer_account_create',
                    'customer_account_edit'
                ]]);
            $attribute->save();


            $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'codice_univoco', [
                'type' => 'varchar',
                'label' => 'Codice Univoco',
                'input' => 'text',
                'source' => '',
                'required' => false,
                'visible' => true,
                'position' => 333,
                'system' => false,
                'backend' => ''
            ]);

            $attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'codice_univoco')
                ->addData(['used_in_forms' => [
                    'adminhtml_customer',
                    'adminhtml_checkout',
                    'customer_account_create',
                    'customer_account_edit'
                ]]);
            $attribute->save();


        }
    }
}

in short, it needs to create 2 new text (varchar) attributes. My module.xml has 

setup_version="1.0.5"  schema_version="1.0.5"

so it should enter the version_compare condition and create the attribute, but, after running php bin/magento setup:upgrade it doesn't work. Even by removing that "if" condition no attributes are created. If i check in the setup_module table, the setup_version and schema_version change correctly with the version in the module.xml. It looks like for some reason the UpgradeData.php does not get executed at all (even trying to log a string into a log file doesn't write anything). Other log files under ..../var/log/ show no errors or warnings. With this module i have already created OTHER custom Customer attributes with the installData.php file with no issues at all. 

I have no idea what to do, can't really understand what i'm doing wrong

 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: [Magento2.2]Creating a custom Customer attribute through module upgrade (UpgradeData.php)

Hello @daniel_bertagnolli 

 

  1. Open database.
  2. Find setup_module table
  3. Find your current module and delete that particular row
  4. Run this command:
    php bin/magento config:set dev/debug/debug_logging 1

If php bin/magento config:set dev/debug/debug_logging 1 command not found,

than

find the below code in [Magento_Root]/app/etc/env.php and set 'debug_logging' to 1

 

'system' => [
       'default' => [
           'dev' => [
               'debug' => [
                   'debug_logging' => '0'
               ]
           ]
       ]
   ]

 

     5. Run this command:

     

php bin/magento system:upgrade  

 
Hope it helps.

---
If you've found my answer useful, please give"Kudos" and "Accept as Solution"

View solution in original post

10 REPLIES 10

Re: [Magento2.2]Creating a custom Customer attribute through module upgrade (UpgradeData.php)

Hello @daniel_bertagnolli 

Please try this solution:

 

Replace:

setup_version="1.0.5"  schema_version="1.0.5"

 With:

setup_version="1.0.6"  schema_version="1.0.6"

 

Hope it helps!

---
If you've found my answer useful, please give"Kudos" and "Accept as Solution"

Re: [Magento2.2]Creating a custom Customer attribute through module upgrade (UpgradeData.php)

Hi @Meetanshi

 

tried and it doesn't work. It changes the module version in the database but no attributes added 

Re: [Magento2.2]Creating a custom Customer attribute through module upgrade (UpgradeData.php)

Hello @daniel_bertagnolli  again,

 

I think you can find the issue if you try debugging. Print log to find what's wrong.

If you need help with that, you may refer How to Print Log in Magento 2

 

Thanks.

---
If you've found my answer useful, please give"Kudos" and "Accept as Solution"

Re: [Magento2.2]Creating a custom Customer attribute through module upgrade (UpgradeData.php)

@Meetanshi I'm trying to have it log something (checked store settings, debug to file enabled, same with the command on the guide) but nothing gets logged for both ->info and ->debug. Looks like the UpgradeData.php doesn't get launched for some reason. Ofcourse every time i launch the setup:upgrade command i change the version in the module.xml file

Re: [Magento2.2]Creating a custom Customer attribute through module upgrade (UpgradeData.php)

Hi @daniel_bertagnolli ,

try to run below command from your magento 2 root and then check your debug log is working or not.

php bin/magento setup:config:set --enable-debug-logging=true && php bin/magento cache:flush
---
If you've found my answer useful, please give"Kudos" and "Accept as Solution"

Re: [Magento2.2]Creating a custom Customer attribute through module upgrade (UpgradeData.php)

@Meetanshi 

Returns an error, apparently --enable-debug-logging is not a valid option 

 [Symfony\Component\Console\Exception\RuntimeException]  
  The "--enable-debug-logging" option does not exist.

Re: [Magento2.2]Creating a custom Customer attribute through module upgrade (UpgradeData.php)

Hello @daniel_bertagnolli 

 

  1. Open database.
  2. Find setup_module table
  3. Find your current module and delete that particular row
  4. Run this command:
    php bin/magento config:set dev/debug/debug_logging 1

If php bin/magento config:set dev/debug/debug_logging 1 command not found,

than

find the below code in [Magento_Root]/app/etc/env.php and set 'debug_logging' to 1

 

'system' => [
       'default' => [
           'dev' => [
               'debug' => [
                   'debug_logging' => '0'
               ]
           ]
       ]
   ]

 

     5. Run this command:

     

php bin/magento system:upgrade  

 
Hope it helps.

---
If you've found my answer useful, please give"Kudos" and "Accept as Solution"

Re: [Magento2.2]Creating a custom Customer attribute through module upgrade (UpgradeData.php)

Hi @Meetanshi,

Sorry for not answering you sooner. I've tried your latest solution but it still doesn't launch the UpgradeData.php script so it doesn't create the new attributes I need

Re: [Magento2.2]Creating a custom Customer attribute through module upgrade (UpgradeData.php)

OK, so i managed to "fix" it. 

In short i took @Meetanshi suggestion about deleting the module's row in the setup_module table in the database but then instead of running the upgrade command i moved the code i had in the UpgradeData.php in the InstallData.php and then ran the magento setup:upgrade command, it worked and the new attributes have been added.

I still don't know why the UpgradeData.php wasn't working so if anyone of you has an idea let me know. I'll still give Meetanshi the solution since part of the solution was his idea