In-depth understanding of magento– Fifth Chapter –magento resource allocation (RPM)

Source: Internet
Author: User
Tags magento upgrade

Fifth Chapter –magento Resource allocation

Maintaining database synchronization in the development and production environments is a headache for any project that is frequently updated. Magento provides a set of systems to address this problem with versioned resource migration scripts.

In the previous chapter, we created a model for Helloworld blogpost. We create a data table directly from the SQL statement "CREATE TABLE". In this chapter, we will create a resource configuration (Setup Resource) for the HelloWorld module to create the data table. We will also create a module upgrade script to upgrade the modules that have already been installed. Here are the steps we'll take

    1. To add a resource configuration in a configuration file
    2. Create a resource class file
    3. Create an installation script
    4. Create an upgrade script
    5. Add Resource Configuration

Modify the CONFIG. HelloWorld model
<resources>
    <!-- ... -->
            <setup>
            <module>Zhlmmc_Helloworld</module>
            <class>Zhlmmc_Helloworld_Model_Resource_Mysql4_Setup</class>
        </setup>
        <connection>
            <use>core_setup</use>
        </connection>
        <!-- ... -->
</resources>

The File: app/code/local/Zhlmmc/Helloworld/Model/Setup/Mysql4/Setup.php
class Zhlmmc_Helloworld_Model_Setup_Mysql4_Setup extends Mage_Core_Model_Resource_Setup {
}

Create an installation script

Below we will create an installation script. This installation script contains SQL statements such as "CREATE TABLE". This script will be run at the initialization of the module. First, let's take a look at the module's configuration file
<modules>
    <Zhlmmc_Helloworld>
        <version>0.1.0</version>
    </Zhlmmc_Helloworld>
</modules>

This section is required for all CONFIG. It contains the name of the module, as well as the version. The name of our installation script will be based on this version number, "0.1.0". Create the following file
File: app/code/local/Zhlmmc/Helloworld/sql/helloworld_setup/mysql4-install-0.1.0.php
echo ‘Running This Upgrade: ‘.get_class($this)."\n <br /> \n";
die("Exit for now"); 

The "Helloworld_setup" in the file path should be consistent with the Running This Upgrade: Zhlmmc_Helloworld_Model_Setup_Mysql4_Setup
Exit for now
...

This means that our installation script has been run. Let's not put the SQL script here, let's go through the process of creating a resource configuration first. Remove the "Die ()" statement, reload the page, you should see your upgrade statement at the top of the page, refresh the page again, the page should appear normally.

Resource version

Magento's resource configuration system allows you to copy the installation script and the upgrade script directly to the server, and Magento automatically runs the corresponding script based on the version of the current module. So you just need to maintain a copy of the database migration script. Let's take a look at the "Core_resource" data sheet.
Mysql> select Code,version from Core_resource;
+ ————————-+ ———— +
| Code | Version |
+ ————————-+ ———— +
| Adminnotification_setup | 1.0.0 |
| Admin_setup | 0.7.2 |
| Alipay_setup | 0.9.0 |
| Api_setup | 0.8.1 |
| Backup_setup | 0.7.0 |
| Bundle_setup | 0.1.11 |
| Canonicalurl_setup | 0.1.0 |
| Catalogindex_setup | 0.7.10 |
| Cataloginventory_setup | 0.7.5 |
| Catalogrule_setup | 0.7.8 |
| Catalogsearch_setup | 0.7.7 |
| Catalog_setup | 1.4.0.0.21 |
| Checkout_setup | 0.9.5 |
| Chronopay_setup | 0.1.0 |
| Cms_setup | 0.7.13 |
| Compiler_setup | 0.1.0 |
| Contacts_setup | 0.8.0 |
| Core_setup | 0.8.26 |
| Cron_setup | 0.7.1 |
| Customer_setup | 1.4.0.0.6 |
| Cybermut_setup | 0.1.0 |
| Cybersource_setup | 0.7.0 |
| Dataflow_setup | 0.7.4 |
| Directory_setup | 0.8.10 |
| Downloadable_setup | 0.1.16 |
| Eav_setup | 0.7.15 |
| Eway_setup | 0.1.0 |
| Flo2cash_setup | 0.1.1 |
| Giftmessage_setup | 0.7.2 |
| Googleanalytics_setup | 0.1.0 |
| Googlebase_setup | 0.1.1 |
| Googlecheckout_setup | 0.7.3 |
| Googleoptimizer_setup | 0.1.2 |
| Helloworld_setup | 0.1.0 |
| Ideal_setup | 0.1.0 |
| Index_setup | 1.4.0.2 |
| Log_setup | 0.7.7 |
| Moneybookers_setup | 1.2 |
| Newsletter_setup | 0.8.2 |
| Oscommerce_setup | 0.8.10 |
| Paybox_setup | 0.1.3 |
| Paygate_setup | 0.7.1 |
| Payment_setup | 0.7.0 |
| Paypaluk_setup | 0.7.0 |
| Paypal_setup | 0.7.4 |
| Poll_setup | 0.7.2 |
| Productalert_setup | 0.7.2 |
| Protx_setup | 0.1.0 |
| Rating_setup | 0.7.2 |
| Reports_setup | 0.7.10 |
| Review_setup | 0.7.6 |
| Salesrule_setup | 0.7.12 |
| Sales_setup | 0.9.56 |
| Sendfriend_setup | 0.7.4 |
| Shipping_setup | 0.7.0 |
| Sitemap_setup | 0.7.2 |
| Strikeiron_setup | 0.9.1 |
| Tag_setup | 0.7.5 |
| Tax_setup | 0.7.11 |
| Usa_setup | 0.7.1 |
| Weee_setup | 0.13 |
| Widget_setup | 1.4.0.0.0 |
| Wishlist_setup | 0.7.7 |
+ ————————-+ ———— +
All in Set (0.00 sec)
This table contains the versions of all modules and modules installed in the system. You can see our module
| helloworld_setup        | 0.1.0      |
Magento is based on this version to determine whether you need to run the upgrade script. Here "Helloworld_setup" version "0.1.0", and our installation script is "0.1.0", so Magento no longer run the script. If you need to rerun the installation script (commonly used during development), simply delete the data from the corresponding module in the table. Let's try it.
DELETE from Core_resource where code = ' Helloworld_setup ';
This time we're going to create a data table by installing a script, so we're going to delete the data table we created.
drop TABLE blog_posts;
Add the following code to our installation script
$ Installer = $this;
$installer->startsetup ();
$installer->run ("
    CREATE TABLE ' {$installer->gettable (' Helloworld/blogpost ')} ' (
      ' blogpost_id ' int (one) not NULL auto_increment,
      ' title ' text,
      ' post ' text,
      ' Date ' datetime Default NULL,
      ' timestamp ' timestamp not NULL default Current_timestamp,
 & nbsp;    PRIMARY key  (' blogpost_id ')
   ) Engine=innodb DEFAULT Charset=utf8;

INSERT into ' {$installer->gettable (' Helloworld/blogpost ')} ' VALUES (1, ' My New Title ', ' This is a blog post ', ' 2009-07-01 00:00:00 ', ' 2009-07-02 23:12:30 ');
");
$installer->endsetup ();
Emptying the Magento cache and accessing any URL, you should find that the "blog_posts" table has been built again, with a single piece of data.

Anatomy configuration Script

Let's examine the code above. The Translator notes: The author mixed the "Install script", "Upgrade Script" and "setup Script" in the text. I try to distinguish between the translation. The configuration script contains the installation script and the upgrade script. "See First line first
$installer = $this;
What is this "$this"? Each configuration script belongs to a resource configuration class (Setup Resource Class), such as the "Zhlmmc_helloworld_model_setup_mysql4_setup" we created above. These scripts are run in the context of the resource configuration class. So "$this" means an instance of the resource configuration class. Although not mandatory, most of the core module's resource configuration classes use "$installer" instead of "$this", just as we do here. Although we say it's not mandatory, we'd better keep this promise unless you have a good reason.

Next look at the following two calls
$installer->startsetup ();
...
$installer->endsetup ();
If you open the source code for 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 do some SQL prep work
Public Function Startsetup ()
{
$this->_conn->multi_query ("SET sql_mode=";
SET @[email protected] @FOREIGN_KEY_CHECKS, foreign_key_checks=0;
SET @[email protected] @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 true 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, separated by semicolons. You may have noticed the following statement
$installer->gettable (' helloworld/blogpost ')
" Translator Note: What is your first reaction? Is it reminiscent of the following code in CONFIG.
   <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 that we are creating. Magento first find the module with "HelloWorld", get the Resource module "HELLOWORLD_MYSQL4", and then find the class name "Blog_posts" through "blogpost" under the resource module. "Obviously, you can use the constant" blog_posts "instead of calling" getTable ", but calling this method ensures that even if the user changes the data table name in the configuration file, your configuration script will still work. The "Mage_core_model_resource_setup" class has a lot of help functions like this. The best way to learn these functions is to read the resource configuration Class code of the Magento core module.

Module Upgrade Script

As we said above, the configuration script includes the installation script and the upgrade script. After the installation script is finished, let's talk about the upgrade script. The installation script is used the first time the module is installed, and the upgrade script is the name of the upgrade module. The Magento resource configuration system uses a versioned approach to upgrade modules.

The first thing to note is that Magento executes an installation script when the module is installed, and Magento no longer executes any installation scripts for that module. If you want to update the data table of the module, it will be executed by the upgrade script. In addition to naming, upgrade scripts and installation scripts are not much different.

Create the upgrade script 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 Versi On number <br/> ';
Die ();
Upgrade scripts and installation scripts are placed under the same directory, but are named differently. The first is the "Upgrade" keyword. Second, you will find here that we have two version numbers, separated by "-". The first version number "0.1.0" refers to which version is upgraded (the starting version), and the second version number "0.2.0" refers to which version (target version) to upgrade to.

Empty the Magento cache, request any URL, and you will see that no configuration scripts are running. That's because, first, we've run the installation script, and second, the current version of our module is "0.1.0", so Magento won't run the upgrade script we're upgrading to "0.2.0". To have Magento run this upgrade script, we have to modify the version number in the configuration file
<modules>
    <zhlmmc_helloworld>
        <version>0.2.0</version>
    </zhlmmc_helloworld>
</modules>
Empty the Magento cache, re-request the page, and you will see the output of the upgrade script. "Translator Note: The author here introduced the second upgrade to" 0.1.5 "upgrade script, I think it is not necessary, I would like to directly summarize the steps of Magento upgrade

    1. Get the current module's installation version from the data table "Core_resource"
    2. Get the current module version from the configuration file
    3. If two versions are the same, then nothing is done
    4. If the version number of # # is less than the # # version number, I don't know what Magento would do, which is theoretically impossible.
    5. If the version number of # # is greater than the # # version number, start the upgrade program
    6. In the Config script folder ("Helloworld_setup" in the example above), add all the upgrade scripts to the queue
    7. Within the queue, sort by the starting version of the upgrade script, ascending
    8. Loop queue
    9. 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 the script
    10. If the starting version of the current script in the queue equals the version number of the current module in the "Core_resource" datasheet and the target version is less than or equal to the version number of # #, the script is executed.
    11. End of loop queue, end of upgrade

It is important to note that the 10th step, each time you execute an upgrade script, the version number in the "Core_resource" datasheet will be updated. So if we have two upgrade files "0.1.0-0.1.5" and "0.1.0-0.2.0", only one upgrade file will be executed.

Let's add the actual content to the upgrade script.
$installer = $this;
$installer->startsetup ();
$installer->run ("
ALTER TABLE ' {$installer->gettable (' Helloworld/blogpost ')} '
Change post post, text not null;
");
$installer->endsetup ();
Empty the Magento cache, refresh the page, and you should see the output of the upgrade script. If not, please check the Magento upgrade steps above to find the error.

Summarize

In this chapter we explain how Magento handles the installation and upgrading of module data tables. The Magento resource configuration system uses a versioned upgrade script, which ensures that different versions of the module can use the same set of upgrade scripts for ease of maintenance. The following chapters will also mention the advantages of this upgrade approach, especially for modules that use the EAV model.

In-depth understanding of magento– Fifth Chapter –magento resource allocation (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.