第五章 – Magento资源配置1

对于任何一个更新频繁的项目来说,保持开发环境和生产环境的数据库同步是件很头疼的事情。Magento提供了一套系统,用版本化的资源迁移脚本来解决这个问题。

上一章,我们为 Helloworld Blogpost 创建了一个模型。我们直接通过SQL语句“CREATE TABLE”来创建数据表。在这一章,我们将为Helloworld模块创建一个资源配置(Setup Resource)用于创建数据表。我们也会创建一个模块升级脚本,用来升级已经安装的模块。下面是我们要做的步骤

  1. 在配置文件中添加资源配置
  2. 创建资源类文件
  3. 创建安装脚本
  4. 创建升级脚本
  5. 添加资源配置

 

修改Helloworld模型的config.xml

<resources>



<!-- ... -->
<helloworld_setup>
<setup>
<module> Zhlmmc_Helloworld</module>
<class> Zhlmmc_Helloworld_Model_Setup_Mysql4_Setup</class>
</setup>
<connection>
<use> core_setup</use>
</connection>
</helloworld_setup>
<!-- ... -->
</resources>

标签是用来唯一标识我们正在创建的资源配置。虽然不是强制要求,但是我们应该使用 “modelname_setup”来命名资源配置。标签的内容是“Packagename_Modulename”。最后,标签的内容就是我们将要创建的资源配置类的类名。虽然对于基本的配置来说,没有必要创建一个单独的资源配置类,但是为了更好的理解资源配置是如何工作的,我们的例子还是创建一个单独的类。
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 TABLE”等SQL语句。这个脚本将在模块初始化的被运行。首先我们来看一下模块的配置文件

<modules>



<zhlmmc_helloworld>
<version> 0.1.0</version>
</zhlmmc_helloworld>
</modules>

这一部分是所有config.xml都必须包含的。它包含了模块的名称,还有版本。我们的安装脚本的名字将基于这个版本号,“0.1.0”。创建以下文件
File: app/code/local/Zhlmmc/Helloworld/sql/helloworld_setup/mysql4-install-0.1.0.php

echo 'Running This Upgrade: '.get_class($this)."/n   /n";
die("Exit for now");

文件路径中的“helloworld_setup”应该和上文在config.xml中添加的一致。文件名中的“0.1.0”就是模块的版本号。清空Magento缓存,访问任何URL,你应该看到以下内容
Running This Upgrade: Zhlmmc_Helloworld_Model_Setup_Mysql4_Setup
Exit for now

这说明我们的安装脚本已经被运行了。我们先不放SQL脚本在这里,先把创建一个资源配置的流程走完。移除“die()”语句,重新装载页面,你应该看到你的Upgrade语句在页面的顶部,再次刷新页面,页面应该正常显示了。

资源版本

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;
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 ( ) ;

清空Magento缓存,访问任何URL,你应该发现“blog_posts”表又被建立了,拥有一条数据。

来自: http://hi.baidu.com/190420456/blog/item/2a79be02583a7ee309fa9314.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值