3

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?

5
  • Have you try to create custom attribute, right? Jan 26, 2017 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? Jan 26, 2017 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. Jan 15, 2018 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 Jan 16, 2018 at 14:22
  • Please post the content of your InstallSchema.php file and also the row containing your module from the setup_module table. May 1, 2018 at 20:31

4 Answers 4

5

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.

1
  • deleting from setup_module worked for me. Thanks. Sep 21, 2022 at 12:58
2

i found that i was missing setup_version in etc/module.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Name_ModuleName" setup_version="0.0.1">
</module>

if you etc/module is right then run: DELETE FROM setup_module where module = "Module_Name"

run bin/magento setup:upgrade

1

You can change the version of your module or delete the record from the setup_module table.

Then run setup:upgrade and setup:di:compile

0

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

bin/magento setup:di:compile

BEFORE you run setup:upgrade.

Your Answer

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

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