How to make a specific script run only once in the lifetime of an extension
-
11-10-2020 - |
Question
The requirement is that I want to execute a script upon the installation of my extension. But I want to make sure that it runs only once and then never again.
I tried doing this via Setup Resources. But apparently, it runs not only when the user installs the extension but also when the clears the cache and other actions etc.
Setup.php
class My_Module_Model_Resource_Setup extends Mage_Core_Model_Resource_Setup
{
function __construct($arg)
{
# Code that I want to run only once
parent::__construct($arg);
}
}
Is there a specific way to achieve this without having to write stuff to the database?
Solution
Don't use the constructor for that.
Using the constructor will make it run every time you instantiate the class and that happens each time the module is checked to see if an upgrade is needed.
Let's say your module declared version in config.xml
is 1.0.0
.
You need to create the file sql/[module]_setup/install-1.0.0.php
where you can run your script.
This is procedural coding so you don't need to create classes. But $this
still references the class My_Module_Model_Resource_Setup
.