1

I want to create attribute programmatically. I've tested few codes but all of them doesn't work for me.

Here are my steps:

  1. Create InstallData.php file with code like this one below.

    • IN InstallData.php my PhpStorm highlights "undefined class EavSetupFactory"
  2. I use command in magento root folder "php bin/magento setup:upgrade". -

    • IN InstallData.php my PhpStorm highlights "undefined class EavSetupFactory"
    • After that i see "Please re-run Magento compile command"
    • I checked admin panel there is no new attributes
  3. Next step i use "php bin/magento setup:di:compile".

    • IN - InstallData.php now its ok, no comunicate with "undefined class EavSetupFactory" i can redirect to this class
    • Check admin panel, still no attributes
  4. Again use "php bin/magento setup:upgrade".

    • IN - InstallData.php my PhpStorm highlights "undefined class EavSetupFactory"
    • Still no new attributes in admin panel

I repeated my steps with different examples of code in InstallData.php. As I mentioned above this is just one of many I've used, they are quite similar (but still there are some small differences) and file look like this:

namespace LCB\Import\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory; //here is mentioned highlight
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{

private $eavSetupFactory;

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

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'test_attribute',
        [
            'type' => 'int',
            'backend' => '',
            'frontend' => '',
            'label' => 'Test Attribute',
            'input' => '',
            'class' => '',
            'source' => '',
            'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,
            'visible' => true,
            'required' => false,
            'user_defined' => false,
            'default' => 0,
            'searchable' => false,
            'filterable' => false,
            'comparable' => false,
            'visible_on_front' => false,
            'used_in_product_listing' => true,
            'unique' => false,
            'apply_to' => ''
        ]
    );
}
}
1

Do you already have LCB_Import installed? Check setup_module table for this entry. If there is one InstallData won't get fired. You need to move the code to UpgradeData and bump module version. If not then first clear cache and then run php bin/magento module:enable LCB_Import and after that php bin/magento setup:upgrade (although setup:upgrade should enable all modules that are yet not in app/etc/config.php file).

Factory classes are generated on the fly in developer mode or during setup:di:compile command. So PhpStorm is showing you undefined class because it has not yet been generated in var/generation folder.

  • I had installed and enabled my module earlier in the same way as you mentioned. There is entry in setup_module table with LCB_Import. It seems that factory classes are generated during setup:di:compile and setup:upgrade is deleting them. So I need to move above code to UpgradeData and do setup:upgrade? (I'll try it now) – mark Apr 18 '17 at 6:52
  • I think I created module correctly but I have an idea, because I'm new to Magento maybe I missed something obvious during creation of module or InstallData? – mark Apr 18 '17 at 9:06
  • @mark, yes if you've already installed a module new updates in install scripts need to be put inside UpgradeData/UpgradeSchema.php. Check this docs for reference: devdocs.magento.com/guides/v2.1/extension-dev-guide/prepare/… – Zefiryn Apr 18 '17 at 11:15

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.