Question

I've made several custom modules for i.e. adding attributes to categories. This is all the first time for me, so I set up the module the first time, enabled it and all is fine so far.

But when I go back to make changes in the code, I don't see them happening (and I'm talking about changing the text in a label, so it should be visible). I figured it has to do something with the way the module is implemented, so I tried;

  • Disabling/Enabling the module
  • Clearing cache / var/ folder
  • Running setup:static-content:deploy
  • Running setup:upgrade
  • Running setup:db-schema:upgrade and setup:db-data:upgrade
  • Changing the module version in module.xml from 1.0.0 to 1.0.1
  • Running indexer:reindex

But still I don't see my changes in the module. What must I do to 'apply' the changes in a module? For the creation of an attribute, I use a Setup/InstallData.php file. Could it be the data is setup once, and doesn't get "refreshed" with an edit? What would be the best way to go around this (maybe changing the name, deleting in the DB)?

edit: I've now also changed the field-id and redid all steps above, still to no avail :/

update: this is the tutorial I used to get where I am; http://www.ibnab.com/en/blog/magento-2/magento-2-add-custom-eav-attribute-to-category-or-customer

And to test it, at first I created a text-field as attribute. That works, now I want to change it to a media-field (like a second category image field). This is the (part of) code in question ( Setup/InstallData.php ):

    $categorySetup->removeAttribute(\Magento\Catalog\Model\Category::ENTITY, 'cat_bgimg1');
    $categorySetup->addAttribute(
    \Magento\Catalog\Model\Category::ENTITY, 'cat_bgimg1', [
         'type' => 'file',
         'label' => 'Category backgroundimage',
         'input' => 'file',
         'source' => '',
         'visible_on_front' => true,
         'required' => false,
         'sort_order' => 100,
         'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
         'group' => 'General Information',
    ]

Update To try to force the changes I disabled the module, I updated module.xml to 1.0.1 <module name="MyModule" setup_version="1.0.1"> and re-enabled the module, ran setup:upgrade etc. In the database table setup_module the new version was added, but the changes in code weren't applied. Maybe this is due me only having an InstallData script, and no updatescript.

Update 3 lord_of_strings gave me the right info on how this works and why it works like this. I ended up just clearing everything out and adding it again.

Was it helpful?

Solution

If you have your attribute in adminhtml it means that you have used your sql script (InstalData.php) at least once. Sql scripts are run when you run setup:upgrade.

Take a look at table setup_module. Here you can find all modules and their versions. You should also find yours. There is a column data_version or something like that. If you want to modify our attribute programatically (from sql script) you have to delete row with your module version and run setup:upgrade again. Before it, you should also delete your attribute (I think the best way to do it is from adminhtml, not directly in DB). Than when command is executed you should have your attribute with changes, of course if they are correct.

There is other way to make changes - Upgrade script, then apart preparing script itself you have to change your module version for higher one.

Take a look at this: http://inchoo.net/magento-2/setup-scripts-magento-2/

OTHER TIPS

Sounds strange, but one more thing you can try is making sure you are in developer mode when you are developing your extensions.

php bin/magento deploy:mode:show

If it doesn't say developer, you can use

php bin/magento deploy:mode:set developer

Your code might also be subject to opcache to clear this you can either change settings in php or run the following php function on your server:

<?php

opcache_reset();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top