Anatomy configuration script
Let's analyze the above Code. [Translator's note: The authors mix "Install script", "upgrade script", and "setup script ". I try my best to distinguish between them during translation. The configuration script contains the installation script and upgrade script .] First look at the first line
$installer = $this;
What is "$ This? Every configuration script belongs to a resource configuration class (Setup Resource
Class), for example, the "zhlmmc_helloworld_model_setup_mysql4_setup" created above ". These scripts are all in this resource.
The configuration class runs in the context environment. Therefore, "$ this" refers to the instance of the resource configuration class. Although not mandatory, most core module resource configuration classes use "$ installer"
To replace "$ this", just like what we do here. Although we say it is not mandatory, we 'd better follow this Agreement unless you have a good reason.
Next, let's take a look at the following two calls:
$installer->startSetup();
//…
$installer->endSetup();
If you open the source code of the "mage_core_model_resource_setup" Class (that is, the parent class of the resource configuration class we created), you will see that these two methods have made some SQL preparation work.
public function startSetup()
{
$this->_conn->multi_query("SET SQL_MODE='';
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';
");
return $this;
}
public function endSetup()
{
$this->_conn->multi_query("
SET SQL_MODE=IFNULL(@OLD_SQL_MODE,'');
SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS,0);
");
return $this;
}
The real configuration logic is in the "run" method.
$installer->run(…);
The parameter of this method is the SQL you want to run. You can include any number of SQL statements separated by semicolons. You may have noticed the following statements:
$installer->getTable('helloworld/blogpost')
[Translator's note: What is your first response? Do you think of the following code in config. xml?
<class>Zhlmmc_Helloworld_Model_Resource_Mysql4</class>
<entities>
<blogpost>
<table>blog_posts</table>
</blogpost>
</entities>
Here "helloworld/blogpost" is the URI of the data table we want to create. Magento first uses "helloworld" to find the module and obtain the resource module.
"Helloworld_mysql4", and then find the class name "blog_posts" under the Resource module through "blogpost ".] Obviously, you can use Constants
"Blog_posts" instead of "gettable", but calling this method ensures that your configuration script can still run even if you have changed the name of the data table in the configuration file.
The "mage_core_model_resource_setup" class has many helper functions like this. The best way to learn these functions is to read the magento core module.
Resource configuration class code.
Module upgrade script
As mentioned above, the configuration script includes the installation script and upgrade script. After completing the installation script, let's talk about the upgrade script. The installation script is used when the module is installed for the first time. As the name suggests, the upgrade script is used when the module is upgraded. Magento's resource configuration system uses versioning to upgrade modules.
The first thing to note is that magento will execute an installation script when installing the module, and magento will no longer execute any installation script for the module. If you want to update the data table of the module, run the upgrade script. Except for the naming method, the upgrade script and installation script are not much different.
The upgrade script is as follows:
File: app/code/local/Zhlmmc/Helloworld/sql/helloworld_setup/mysql4-upgrade-0.1.0-0.2.0.php
echo 'Testing our upgrade script (mysql4-upgrade-0.1.0-0.2.0.php) and halting execution to avoid updating the system version number ';
die();
The upgrade script and installation script are stored in the same directory, but the naming method is different. The first is the keyword "Upgrade. Next, you will find that we have two versions separated. First
Version "0.1.0" indicates the version from which the upgrade is made (the initial version), and the second version "0.2.0" indicates the version to which the upgrade is made (the target version ).
Clear magento cache and request any URL. You will find that no configuration script is running. This is because, first, we have run the installation script. Second, the current version of our module is
"0.1.0", so magento will not run the upgrade script we want to upgrade to "0.2.0. To let magento run this upgrade script, we need to modify the version number in the configuration file.
<modules>
<zhlmmc_helloworld>
<version>0.2.0</version>
</zhlmmc_helloworld>
</modules>
Clear the magento cache and re-request the page. You will see the output of the upgrade script. [Translator's note: I introduced the second upgrade script to "0.1.5" here. I don't think it is necessary. I will summarize the magento upgrade steps directly.
- Obtain the installation version of the current module from the data table "core_resource ".
- Obtain the current module version from the configuration file
- If the two versions are the same, do nothing.
- If the version number of #2 is smaller than the version number of #1, I do not know what magento will do. Theoretically, this is impossible.
- If the version number of #2 is greater than the version number of #1, upgrade the program.
- Add all the upgrade scripts to the queue in the configuration script folder ("helloworld_setup" in the preceding example)
- In the queue, sort by the START version of the upgrade script in ascending order.
- Cyclic queue
- If the starting version of the current script in the queue is not equal to the version number of the current module in the "core_resource" data table, skip this script.
- If the starting version of the current script in the queue is equal to the version number of the current module in the "core_resource" data table and the target version is less than or equal to the version number of #2, run the script.
- The cyclic queue ends and the upgrade ends.
It is worth noting that the version number in the "core_resource" data table is updated every time an upgrade script is executed in step 1. Therefore, if we have two upgrade Files "0.1.0-0.1.5" and "0.1.0-0.2.0", only one file will be executed.
]
Next we will add substantive content for the upgrade script.
$installer = $this;
$installer->startSetup();
$installer->run("
ALTER TABLE `{$installer->getTable('helloworld/blogpost')}`
CHANGE post post text not null;
");
$installer->endSetup();
Clear magento cache and refresh the page. You should see the upgrade Script output. If no, please check the magento upgrade steps described above for an error.
Summary
This chapter describes how magento handles the installation and upgrade of module data tables. Magento Resource Configuration System Version Upgrade
Level script to ensure that different versions
The module can use the same set of upgrade scripts for easy maintenance. We will also discuss the advantages of this upgrade method in later sections, especially for modules using the EAV model.
For your security, please only open the URL with reliable source
Open website
Cancel
From: http://hi.baidu.com/190420456/blog/item/5d677fd2208f680f3bf3cf15.html