6

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.

1
  • 1
    if you change version, without adding upgrade script it won't work. In upgrade script any changes you wish to perform put inside ` ` if (version_compare($context->getVersion(), '1.0.5') < 0) {}` and then it should work Commented Jun 17, 2016 at 13:42

2 Answers 2

7

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/

9
  • That's a very helpful source in understanding all this. Thanks! I'll go jump in to it and get back to you.
    – Asitis
    Commented Jun 17, 2016 at 9:08
  • Ok! If I understand correctly, if my module would have Setup/UpgradeSchema Setup/UpgradeData scripts, it would recheck everytime I run setup:upgrade. Since I only need to add the attribute once, this seems unnecessary. I see in the database that the data_version is indeed 1.0.1. And such I find it strange that there is no automatic-recheck if the version is updated. So my best course of action in this case would be to just remove the thing entirely from the database, and run it again (?)
    – Asitis
    Commented Jun 17, 2016 at 9:43
  • There is check, but if version isn't higher. If it is higher than in db than code for this version inside ` if (version_compare($context->getVersion(), '1.0.5') < 0) {}` is executed (in upgrade script) So, if you want to change something in current version of module - for example type of attribute you should erase row with your module from the db table setup_module and run script setup_upgrade (only install script) Second option is Upgrade script and here for higher version apply some changes according to version no in module.xml. Then you don't have to erase anything from db. Commented Jun 17, 2016 at 10:08
  • But I tried to update the version from 1.0.0 to 1.0.1, and still the changes weren't applied. So apparently when updating a version, the system doesn't check the InstallData again.
    – Asitis
    Commented Jun 17, 2016 at 10:10
  • can you update your answer - with code before and after change? Commented Jun 17, 2016 at 10:25
0

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();
1
  • I'm running in developer mode, yeah. Also just tried the opache_reset to no avail. I'm updating my question with more detail, maybe there is something else in the works..
    – Asitis
    Commented Jun 16, 2016 at 14:04

Your Answer

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

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