原文链接http://alanstorm.com/magento_setup_resources
Magento 设置资源
在任何一个快速迭代的工程下,如何保证开发和生产(现网)数据库同步是一个很头疼的事情。Magento提供了一个创建资源迁移版本的系统,可以帮助我们处理开发过程中不断遇到的这个问题。
上次我们创建了weblogpost的模型。这次,我们执行直接执行CREATE TABLE。我们将未我们的module创建一个Setup Resource,而该资源会创建一个表格。我们同时也会创建一个升级的脚本,它能升级已经安装的module。总的来说
1. 在config里增加SetupResource
2. 创建resourceclass文件
3. 创建installerscript
4. 创建升级script
增加Setup Resource
我们在<resource/>部分增加下面的
<resources>
<!-- ... -->
<weblog_setup>
<setup>
<module>XStarX_Weblog</module> <class>XStarX_Weblog_Model_Resource_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</weblog_setup>
<!-- ... -->
</resources>
<weblog_setup>标签是用来唯一表示SetupResource的。通常鼓励使用modelname_setup。<module>XStarX_Weblog</modul>标签下应该包含我们模块的Pachagename_Modulename。最后<class>XStarX_Weblog_Model_Resource_Mysql4_Setup</class>应该包含我们要创建的Setup Resource类的名字。对于基本的脚本来说,没有必要创建自己的类,但是这么做,以后可以更灵活。
增加完配置后,清除cache,并且加载Magento Site,你会发现出异常了
Fatalerror: Class 'XStarX_Weblog_Model_Resource_Mysql4_Setup' not found in
Magento试图实例化我们在config里声明的类,但是没有找到。我们需要创建这样的类文件app/code/local/XStarX/Weblog/Model/Resource/Mysql4/Setup.php
classXStarX_Weblog_Model_Resource_Mysql4_Setup extendsMage_Core_Model_Resource_Setup { }
现在重新加载Magento网站,异常就消失了。
创建安装脚本
接下来,我们要创建安装脚本。脚本包含了之前的CREATETABLE语句。
首先,先看一下config.xml
<modules>
<XStarX_Weblog>
<version>0.1.0</version>
</XStarx_Weblog>
</modules>
这个部分在配置文件中是必备的,标示了module的同时也告诉了版本。安装脚本要基于版本好。在下列位置创建文件
app/code/local/XStarX/Weblog/sql/weblog_setup/mysql4-install-0.1.0.php
echo 'Running This Upgrade: '.get_class($this)."\n<br /> \n";
die("Exit for now");
路径的weblog_setup部分匹配了config.xml文件<weblog_setup/>。0.1.0部分匹配了module的版本。清除缓存,加载页面,可以看到
Running This Upgrade:Alanstormdotcom_Weblog_Model_Resource_Mysql4_Setup Exit for now ...
这意味着我们的update脚本执行了。最终我们把SQL更新文件放在这里,但是暂时我们把精力放在setup机制上。把die声明去掉,
echo 'Running This Upgrade:'.get_class($this)."\n <br /> \n";
重新加载页面,可以看到升级消息在页面的首部分展示。重新加载,页面将恢复正常。因为setup就一次嘛。不可能总setup。
创建安装脚本
MagenoSetup Resources容许我们简单的放置安装脚本和升级脚本,然后系统就会自动执行。这容许我们系统中的数据迁移脚本保持一次。
使用database client,查看core_resroucetable
mysql> select * from core_resource;
+-------------------------+---------+ |code | version |+-------------------------+-----+
|adminnotification_setup | 1.0.0 |
| admin_setup | 0.7.1 |
| amazonpayments_setup | 0.1.2 |
| api_setup | 0.8.1 |
| backup_setup | 0.7.0 |
| bundle_setup | 0.1.7 |
| catalogindex_setup | 0.7.10 |
| cataloginventory_setup | 0.7.5 |
| catalogrule_setup | 0.7.7 |
| catalogsearch_setup | 0.7.6 |
| catalog_setup | 0.7.69 |
| checkout_setup | 0.9.3 |
| chronopay_setup | 0.1.0 |
| cms_setup | 0.7.8 |
| compiler_setup | 0.1.0 |
| contacts_setup | 0.8.0 |
| core_setup | 0.8.13 |
| cron_setup | 0.7.1 |
| customer_setup | 0.8.11 |
| cybermut_setup | 0.1.0 |
| cybersource_setup | 0.7.0 |
| dataflow_setup | 0.7.4 |
| directory_setup | 0.8.5 |
| downloadable_setup | 0.1.14 |
| eav_setup | 0.7.13 |
| 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 |
| ideal_setup | 0.1.0 |
| log_setup | 0.7.6 |
| newsletter_setup | 0.8.0 |
| oscommerce_setup | 0.8.10 |
| paybox_setup | 0.1.3 |
| paygate_setup | 0.7.0 |
| payment_setup | 0.7.0 |
| paypaluk_setup | 0.7.0 |
| paypal_setup | 0.7.2 |
| poll_setup | 0.7.2 |
| productalert_setup | 0.7.2 |
| protx_setup | 0.1.0 |
| rating_setup | 0.7.2 |
| reports_setup | 0.7.7 |
| review_setup | 0.7.4 |
| salesrule_setup | 0.7.7 |
| sales_setup | 0.9.38 |
| sendfriend_setup | 0.7.2 |
| shipping_setup | 0.7.0 |
| sitemap_setup | 0.7.2 |
| strikeiron_setup | 0.9.1 |
| tag_setup | 0.7.2 |
| tax_setup | 0.7.8 |
| usa_setup | 0.7.0 |
| weblog_setup | 0.1.0 |
| weee_setup | 0.13 |
| wishlist_setup | 0.7.4 |
+-------------------------+---------+ 59 rowsin set (0.00 sec)
这个表格包含了所有安装module的list,同时还有对应的版本。在表的结尾部分看到了
| weblog_setup | 0.1.0 |
这个就是Magento如何知道要不要重新执行脚本。如果都成功,页面就会加载。Weblog_setup已经安装了,所以不需要更新。如果想重装脚本,需要删除表里的改行。我们现在可以删除
DELETE from core_resource where code = 'weblog_setup';
然后删除对应的table
DROP TABLE blog_posts;
接着在setup脚本里增加
$installer = $this;
$installer->startSetup();
$installer->run("
CREATE TABLE `{
$installer->getTable('weblog/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=InnoDBDEFAULT CHARSET=utf8;
INSERTINTO `{$installer->getTable('weblog/blogpost')}` VALUES (1,'My NewTitle','This is a blog post','2009-07-01 00:00:00','2009-07-02 23:12:30'); ");
$installer->endSetup();
清除cache,加载页面,你可以看到blog_posts又创建了,并且有一条数据。

本文介绍Magento模块资源迁移机制,包括创建资源迁移版本的系统、安装脚本和升级脚本的方法,以及Magento如何自动执行这些脚本来确保开发和生产数据库同步。
278

被折叠的 条评论
为什么被折叠?



