0

I have created an install script.

But my requirement is, this script has to be run only when it is invoked manually. I have created a button in admin back-end. When I click that button the script has to be run. I have searched in Google. But I can't get any answer.

I don't know is it possible to do like this. How to prevent a script to be execute when that module loads first time.

Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
  • 1
    Magento install scripts (those in namespace/module/sql/module_setup for example) run automatically the first time the module is loaded. You can't run it conditionally. You can however move your installation code to a model and run it from an admin controller that your button loads. –  May 28 '15 at 12:28
  • How to move install script into model. I dont know how to do tat – Krishnan Daiva Jun 01 '15 at 01:18
  • Create a setup model under Model/Resource/Setup.php and put your code there. –  Jun 01 '15 at 03:06
  • Ok. But what should be the base class? – Krishnan Daiva Jun 01 '15 at 05:38
  • startSetup(); $table=$installer->getConnection()->newTable($installer->getTable('ziffity_daiva/daiva'))>addColumn('id',Varien_Db_Ddl_Table::TYPE_INTEGER,null,array( 'identity' => true, 'nullable' => false, 'primary' => true ),'Id') >addColumn('customerid',Varien_Db_Ddl_Table::TYPE_INTEGER,null,array( 'nullable' => false,),'Customer Id') ->addColumn('customername',Varien_Db_Ddl_Table::TYPE_TEXT,null,array( 'nullable' => false,),'Customer Name'); $installer->getConnection()->createTable($table); $installer->endSetup(); – Krishnan Daiva Jun 01 '15 at 05:46
  • above code is my script – Krishnan Daiva Jun 01 '15 at 05:48
  • Try Mage_Core_Model_Resource_Setup. Add a method to this model that you can invoke from your controller. –  Jun 01 '15 at 12:20

1 Answers1

0

Create your own module, and invoke your custom script through the controller on button click. [Managed Way]

OR,

Create a PHP Shell script file with your scripts, and invoke through php *.php terminal command on button click.

Sweet and Simple.

UPDATE

Learn how to create PHP Shell script in Magento, link : http://inchoo.net/magento/php-shell-scripts-for-magento/

Use something like this in your file:

<?php

// YOUR CODE

$output = shell_exec('php myCustomPHPInstallScript.php');
// myCustomPHPInstallScript.php is name of your PHP file.
Mage::log($output, null, 'myCustomLogFile.log'); 
//creating log file

//YOUR CODE

?>
anz
  • 987
  • 7
  • 21
  • how to invoke from custom module? normally script will run automatically right? – Krishnan Daiva May 29 '15 at 07:02
  • No dude, if scipts are placed as cron jobs, then they are automatically fired. Look into http://php.net/manual/en/function.shell-exec.php to learn more about issuing commands from PHP code... – anz May 29 '15 at 11:23
  • this is okay. but how to stop a script when that module is loading? It should not create any entry in core_resource table – Krishnan Daiva May 29 '15 at 12:30
  • No, the script will not load when the module is running, in fact the you don't need to run the module, running any part of magneto installation is enough. It will not create anything in magento db unless u do it explicitly. The version data will be stored in core-resource table if its part of a standard magento module. This is done through the version number in the modules config file, and consequently searches for corresponding db script file in SQL folder of the module. You need to get your magento basics right. – anz May 30 '15 at 15:51
  • I think you did not get my question. If there is a module with install script, if i run the magento, the install script in the module will run automatically and creates the table. I don't want this. But i want to run this install script only when a button is clicked from admin. That is admin has to run that install script – Krishnan Daiva Jun 01 '15 at 01:28
  • Then, as I said before, create a PHP file with your upgrade script, and run through CLI commands. – anz Jun 01 '15 at 05:05
  • But my customer want do it with magento module. – Krishnan Daiva Jun 01 '15 at 05:28