Set default value for attribute in Setup Script in Magento 2












0















I have created install script to add an attribute to category.



Default value has to be set, thus 'default' => '1', is used.



InstallData.php file:



    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
Category::ENTITY,
'my_attr',
[
'type' => 'int',
'label' => 'My',
'input' => 'select',
'default' => '1',
'source' => 'MagentoEavModelEntityAttributeSourceBoolean',
'required' => false,
'sort_order' => 100,
'group' => 'General',
]
);
}


The problem is that later when I use $cat->getMyAttr() I can get this only when I have set it to true or false in admin panel and saved settings. Otherwise I get just null.



In other words default value is not being set during setup of the module.
How to set default value that will be set to all the categories with setup script?










share|improve this question



























    0















    I have created install script to add an attribute to category.



    Default value has to be set, thus 'default' => '1', is used.



    InstallData.php file:



        public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
    /** @var EavSetup $eavSetup */
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
    $eavSetup->addAttribute(
    Category::ENTITY,
    'my_attr',
    [
    'type' => 'int',
    'label' => 'My',
    'input' => 'select',
    'default' => '1',
    'source' => 'MagentoEavModelEntityAttributeSourceBoolean',
    'required' => false,
    'sort_order' => 100,
    'group' => 'General',
    ]
    );
    }


    The problem is that later when I use $cat->getMyAttr() I can get this only when I have set it to true or false in admin panel and saved settings. Otherwise I get just null.



    In other words default value is not being set during setup of the module.
    How to set default value that will be set to all the categories with setup script?










    share|improve this question

























      0












      0








      0








      I have created install script to add an attribute to category.



      Default value has to be set, thus 'default' => '1', is used.



      InstallData.php file:



          public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
      {
      /** @var EavSetup $eavSetup */
      $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
      $eavSetup->addAttribute(
      Category::ENTITY,
      'my_attr',
      [
      'type' => 'int',
      'label' => 'My',
      'input' => 'select',
      'default' => '1',
      'source' => 'MagentoEavModelEntityAttributeSourceBoolean',
      'required' => false,
      'sort_order' => 100,
      'group' => 'General',
      ]
      );
      }


      The problem is that later when I use $cat->getMyAttr() I can get this only when I have set it to true or false in admin panel and saved settings. Otherwise I get just null.



      In other words default value is not being set during setup of the module.
      How to set default value that will be set to all the categories with setup script?










      share|improve this question














      I have created install script to add an attribute to category.



      Default value has to be set, thus 'default' => '1', is used.



      InstallData.php file:



          public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
      {
      /** @var EavSetup $eavSetup */
      $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
      $eavSetup->addAttribute(
      Category::ENTITY,
      'my_attr',
      [
      'type' => 'int',
      'label' => 'My',
      'input' => 'select',
      'default' => '1',
      'source' => 'MagentoEavModelEntityAttributeSourceBoolean',
      'required' => false,
      'sort_order' => 100,
      'group' => 'General',
      ]
      );
      }


      The problem is that later when I use $cat->getMyAttr() I can get this only when I have set it to true or false in admin panel and saved settings. Otherwise I get just null.



      In other words default value is not being set during setup of the module.
      How to set default value that will be set to all the categories with setup script?







      magento2 category attributes eav category-attribute






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Oct 21 '17 at 17:50









      TheKitMurkitTheKitMurkit

      121112




      121112






















          2 Answers
          2






          active

          oldest

          votes


















          0














          This is because according to Magento your module data is already installed and it doesn't update your changes in the InstallData file unless you change your module version.
          Here's the solution:



          Go to your module directory and look for module.xml file in etc folder and simply change the setup version number so that the Magento knows that it has to update the data. Obviously, the setup version will be higher than current. For ex, if it's 1.0.1 then change it to 1.0.2



          <module name="yourmodule_name" setup_version="1.0.1"/>





          share|improve this answer


























          • Thank's but I had to disable/enable the module before getting my code run, but the UpgradeData runs with your method

            – medmek
            Jan 28 at 10:40



















          0














          Set required to true. This is because when attribute is required, it has a value all the time.






          share|improve this answer








          New contributor




          Raibel Botta Rigores is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.




















            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "479"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f198080%2fset-default-value-for-attribute-in-setup-script-in-magento-2%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            This is because according to Magento your module data is already installed and it doesn't update your changes in the InstallData file unless you change your module version.
            Here's the solution:



            Go to your module directory and look for module.xml file in etc folder and simply change the setup version number so that the Magento knows that it has to update the data. Obviously, the setup version will be higher than current. For ex, if it's 1.0.1 then change it to 1.0.2



            <module name="yourmodule_name" setup_version="1.0.1"/>





            share|improve this answer


























            • Thank's but I had to disable/enable the module before getting my code run, but the UpgradeData runs with your method

              – medmek
              Jan 28 at 10:40
















            0














            This is because according to Magento your module data is already installed and it doesn't update your changes in the InstallData file unless you change your module version.
            Here's the solution:



            Go to your module directory and look for module.xml file in etc folder and simply change the setup version number so that the Magento knows that it has to update the data. Obviously, the setup version will be higher than current. For ex, if it's 1.0.1 then change it to 1.0.2



            <module name="yourmodule_name" setup_version="1.0.1"/>





            share|improve this answer


























            • Thank's but I had to disable/enable the module before getting my code run, but the UpgradeData runs with your method

              – medmek
              Jan 28 at 10:40














            0












            0








            0







            This is because according to Magento your module data is already installed and it doesn't update your changes in the InstallData file unless you change your module version.
            Here's the solution:



            Go to your module directory and look for module.xml file in etc folder and simply change the setup version number so that the Magento knows that it has to update the data. Obviously, the setup version will be higher than current. For ex, if it's 1.0.1 then change it to 1.0.2



            <module name="yourmodule_name" setup_version="1.0.1"/>





            share|improve this answer















            This is because according to Magento your module data is already installed and it doesn't update your changes in the InstallData file unless you change your module version.
            Here's the solution:



            Go to your module directory and look for module.xml file in etc folder and simply change the setup version number so that the Magento knows that it has to update the data. Obviously, the setup version will be higher than current. For ex, if it's 1.0.1 then change it to 1.0.2



            <module name="yourmodule_name" setup_version="1.0.1"/>






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 8 '18 at 6:03









            Piyush

            4,79972053




            4,79972053










            answered Feb 8 '18 at 5:44









            BunnyBunny

            1




            1













            • Thank's but I had to disable/enable the module before getting my code run, but the UpgradeData runs with your method

              – medmek
              Jan 28 at 10:40



















            • Thank's but I had to disable/enable the module before getting my code run, but the UpgradeData runs with your method

              – medmek
              Jan 28 at 10:40

















            Thank's but I had to disable/enable the module before getting my code run, but the UpgradeData runs with your method

            – medmek
            Jan 28 at 10:40





            Thank's but I had to disable/enable the module before getting my code run, but the UpgradeData runs with your method

            – medmek
            Jan 28 at 10:40













            0














            Set required to true. This is because when attribute is required, it has a value all the time.






            share|improve this answer








            New contributor




            Raibel Botta Rigores is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.

























              0














              Set required to true. This is because when attribute is required, it has a value all the time.






              share|improve this answer








              New contributor




              Raibel Botta Rigores is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.























                0












                0








                0







                Set required to true. This is because when attribute is required, it has a value all the time.






                share|improve this answer








                New contributor




                Raibel Botta Rigores is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.










                Set required to true. This is because when attribute is required, it has a value all the time.







                share|improve this answer








                New contributor




                Raibel Botta Rigores is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                share|improve this answer



                share|improve this answer






                New contributor




                Raibel Botta Rigores is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                answered 14 mins ago









                Raibel Botta RigoresRaibel Botta Rigores

                1




                1




                New contributor




                Raibel Botta Rigores is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





                New contributor





                Raibel Botta Rigores is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                Raibel Botta Rigores is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Magento Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f198080%2fset-default-value-for-attribute-in-setup-script-in-magento-2%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Magento 2 controller redirect on button click in phtml file

                    Polycentropodidae