1. 程式人生 > >深入理解Magento – 第七章 – 自定義Magento系統配置

深入理解Magento – 第七章 – 自定義Magento系統配置

Magento的資源配置系統允許你直接拷貝安裝指令碼和升級指令碼到伺服器上,Magento會根據當前模組的版本自動執行相應的指令碼。這樣你就只需 要維護一份資料庫遷移指令碼。我們先來看看“core_resource”資料表
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      |
+————————-+————+
63 rows in set (0.00 sec)
這張表包含了系統中所有安裝的模組和模組的版本。你可以看到我們的模組
| helloworld_setup        | 0.1.0      |

Magento就是根據這個版本來判斷是否需要執行升級指令碼的。這裡“helloworld_setup”版本“0.1.0”,而我們的安裝指令碼也是 “0.1.0”,所以Magento不會再執行該指令碼。如果你需要重新執行安裝指令碼(在開發的時候常用到),只要刪除表中相應模組的資料就行了。讓我們來 試試看
DELETE from core_resource where code = ‘helloworld_setup’;
這次我們將通過安裝指令碼來建立資料表,所以我們也要刪除之前建立的資料表
DROP TABLE blog_posts;
新增以下程式碼到我們的安裝指令碼
$installer = $this;
$installer->startSetup();
$installer->run(”
CREATE TABLE `{$installer->getTable(‘helloworld/blogpost’)}` (
`blogpost_id` int(11) NOT NULL auto_increment,
`title` text,
`post` text,
`date` datetime default NULL,
`timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY  (`blogpost_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;