So let’s create your first installation script for your module!
Let’s put together a quick module declaration file, so Magento knows your module exists.
1
2
3
4
5
6
7
8
9
10
|
<!– app/etc/modules/Namespace_Modulename.xml –>
<?xml version=“1.0″?>
<config>
<modules>
<Namespace_Modulename>
<active>true</active>
<codePool>local</codePool>
</Namespace_Modulename>
</modules>
</config>
|
Installation scripts are based off of the version numbers of modules, so we will need to set this inside your config.xml.
1
2
3
4
5
6
7
8
9
|
<!– app/code/local/Namespace/Modulename/etc/config.xml –>
<?xml version=“1.0″?>
<config>
<modules>
<Namespace_Modulename>
<version>1.0.0</version>
</Namespace_Modulename>
</modules>
</config>
|
Next, you need to tell Magento that you have setup files to run. Magento looks for these in the <resources>
section under the <global>
node of yourconfig.xml. This is the same area that defines core_read, core_setup, core_write, etc.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<!– app/code/local/Namespace/Modulename/etc/config.xml –>
<?xml version=“1.0″?>
<config>
<modules>
<Namespace_Modulename>
<version>1.0.0</version>
</Namespace_Modulename>
</modules>
<global>
<resources>
<modulename_setup>
<setup>
<module>Namespace_Modulename</module>
</setup>
</modulename_setup>
</resources>
</global>
</config>
|
Now that you’ve let Magento know that you want to run setup files, you now need to create some files in the area where Magento wants to look.
The folder you would create would be:app/code/local/Namespace/Modulename/sql/modulename_setup/
If you notice, the modulename_setup folder has the same name as the<modulename_setup> node in the config.xml. Coincidence? Nope! Magento does that on purpose.
Now we have to create our first file. We will have to follow the naming convention we discussed in Part 1. So, our first file name would be install-1.0.0.php. The version number is located in the <version>
node in yourconfig.xml.
1
2
3
4
5
6
7
8
9
|
<?php
// app/code/local/Namespace/Modulename/sql/modulename_setup/install-1.0.0.php
$installer = $this;
$installer->startSetup();
// Do some magic here
$installer->endSetup();
|
So what is all of this in the file? Let’s go through line by line:
1
|
$installer = $this;
|
This is just for better readability when using the install file. (If you don’t know what I mean, try using blocks and view files in magento. That gets confusing real quick). So what is the $this
referencing? It is referencingMage_Core_Model_Resource_Setup
. You can tell it to specify your own class in the config.xml, but that’s for another lesson.
1
|
$installer->startSetup();
|
This runs some basic queries such as disabling foreign key checks, etc. This can also be overridden to do some additional actions such as checking for folders, etc. Check out Varien_Db_Adapter_Pdo_Mysql::startSetup() to get an idea.
1
|
$installer->endSetup();
|
Much the same as the startSetup() method above. It runs some basic queries for housekeeping.
Now you are all setup to start running commands. This includes creating database tables, etc. For now, to ensure it works, let’s throw a quick log file in there, to make sure it ran.
1
2
3
4
5
6
7
8
9
|
<?php
// app/code/local/Namespace/Modulename/sql/modulename_setup/install-1.0.0.php
$installer = $this;
$installer->startSetup();
Mage::log(__FILE__ . ‘ installed.’);
$installer->endSetup();
|
Check your var/log/system.log file and verify that the line did in fact write.
Also, check your database under the core_resource
table and look for the entry modulename_setup under the code column. There should be entries ‘1.0.0’ under the version and data_version columns as well.
If so, success!
Trackbacks
-
[…] Ryan an einer Artikel-Reihe über Upgrade-Skripts. Vier Teile sind bis dato erschienen: Overview, Your First Script, Install New Tables und EAV […]
Overview : Magento Installation Scripts (Cool Ryan)
Previous : Part 1: Overview
Next : Part 3: Installing New Tables
Post from coolryan.com