Magento – Magento 2 : InstallData.php Does Not Seems to be Running While setup:upgrade

-setupattributesinstalldatamagento2

In custom module, I have InstallSchema.php and a InstallData.php files.

InstallSchema.php seems to be running fine, both tables declared in file are created.

But with the InstallData.php nothing happens.

InstallData.php

    <?php
namespace VVV\Comercial\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;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Catalog\Setup\CategorySetupFactory;


class InstallData implements InstallDataInterface
{

  protected $customerSetupFactory;


  private $attributeSetFactory;

    private $categorySetupFactory;

    private $eavSetupFactory;

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


  public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
  {

      $setup->startSetup();

    /** @var CustomerSetup $customerSetup */
    //$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
      $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

      $eavSetup->addAttribute(
          Customer::ENTITY,
          'is_commercial',
          [
              'type' => 'int',
              'label' => __('Commercial user'),
              'input' => 'boolean',
              'required' => false,
              'sort_order' => 100,
              'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
              //'group' => 'General Information',
              //'is_used_in_grid' => true,
              //'is_visible_in_grid' => false,
              //'is_filterable_in_grid' => true,
          ]
      );


      $setup->endSetup();

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

    $attributeSet = $this->attributeSetFactory->create();
    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

    $customerSetup->addAttribute(Customer::ENTITY, 'is_commercial', [
      'type' => 'int',
      'label' => __('Commercial user'),
      "default" => 0,
      'input' => 'boolean',
      'required' => false,
      'visible' => true,
      'user_defined' => true,
      'sort_order' => 1000,
      'position' => 1000,
      'system' => 0,
    ]);

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

    $attribute->save();*/
  }
}

The attribute is not created in eav_attribute table, also setup_module table not filled data_version columna when running setup:upgrade.

There is a way to debug a bit more this kind of stuff?

Why InstallData.php does not seems to be running.

For tests, I delete the row in setup_table then run setup:upgrade.

I have to do something else to be able to run the InstallData.php?

Best Answer

Try the following way:

namespace VVV\Comercial\Setup;

use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
 * Class InstallData
 *
 * @package VVV\Comercial\Setup
 */
class InstallData implements InstallDataInterface
{
    /**
     * Customer setup factory
     *
     * @var \Magento\Customer\Setup\CustomerSetupFactory
     */
    private $customerSetupFactory;

    /**
     * Init
     *
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(
        \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        // Add new customer attribute
        $customerSetup->addAttribute(
            Customer::ENTITY,
            'is_commercial',
            [

                'label'                 => 'Commercial user',
                'input'                 => 'boolean',
                'required'              => false,
                'sort_order'            => 900,
                'visible'               => true,
                'system'                => false,
                'is_used_in_grid'       => false,
                'is_visible_in_grid'    => false,
                'is_filterable_in_grid' => false,
                'is_searchable_in_grid' => false
            ]
        );

        // add attribute to form
        /** @var  $attribute */
        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'is_commercial');
        $attribute->setData('used_in_forms', ['adminhtml_customer']);
        $attribute->save();
    }
}

Make sure your module is deleted from setup_module table before run setup:upgrade

For debug, you can user error_log or magento log system for checking, it's running or not when you install module.

Related Topic