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?

  • Have you try to create custom attribute, right? – Dhiren Vasoya Jan 26 '17 at 13:31
  • I'm trying to create the customer attribute on module install. I even put an exit(); in there but same. Also why does not get filled the column data_version in the setup_module on setup:upgrade? – Gianni Di Falco Jan 26 '17 at 14:10
  • Did you ever find a resolution to this issue? I am running into the same problem myself on Magento 2.2.1. – TomWilsonFL Jan 15 at 22:41
  • No, sorry! Try the comment of jdhildeb it may work. setup:di:compuile could give you errors of modules even before installing them – Gianni Di Falco Jan 16 at 14:22
  • Please post the content of your InstallSchema.php file and also the row containing your module from the setup_module table. – Shawn Abramson May 1 at 20:31

If InstallData.php does not seem to be running, make sure you run

bin/magento setup:di:compile

BEFORE you run setup:upgrade.

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.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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