您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Magento 开发笔记5_PHP教程

2024/3/26 6:32:17发布28次查看
在任何一个快速迭代的工程下,如何保证开发和生产(现网)数据库同步是一个很头疼的事情。magento提供了一个创建资源迁移版本的系统,可以帮助我们处理开发过程中不断遇到的这个问题。www.2cto.com
上次我们创建了weblogpost的模型。这次,我们执行直接执行create table。我们将未我们的module创建一个setup resource,而该资源会创建一个表格。我们同时也会创建一个升级的脚本,它能升级已经安装的module。总的来说
1.     在config里增加setupresource
2.    创建resourceclass文件
3.    创建installerscript
4.    创建升级script
增加setup resource
我们在部分增加下面的
xstarx_weblog             xstarx_weblog_model_resource_mysql4_setup
core_setup
标签是用来唯一表示setupresource的。通常鼓励使用modelname_setup。xstarx_weblog标签下应该包含我们模块的pachagename_modulename。最后xstarx_weblog_model_resource_mysql4_setup应该包含我们要创建的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
0.1.0
这个部分在配置文件中是必备的,标示了module的同时也告诉了版本。安装脚本要基于版本好。在下列位置创建文件
app/code/local/xstarx/weblog/sql/weblog_setup/mysql4-install-0.1.0.php
echo 'running this upgrade: '.get_class($this).\n
\n;
die(exit for now);
路径的weblog_setup部分匹配了config.xml文件。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
\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又创建了,并且有一条数据。
创建安装脚本---问题
上面的安装可能不会那么顺利,在magento1.7下面会报错
mage_eav_exception: can't create table: module_entity
如何解决呢?
debug createentitytables()方法,可以在结尾处看到
$connection->begintransaction(); try {      foreach ($tables as $tablename => $table) {         $connection->createtable($table);     }     $connection->commit(); } catch (exception $e) {    zend_debug::dump($e->getmessage());    $connection->rollback();    throw mage::exception('mage_eav', mage::helper('eav')->__('can\'t create table: %s', $tablename)); }
查看底层错误是:usererror: ddl statements are not allowed in transactions
然后跟进commit函数
/**  * check transaction level in case of ddl query  *  * @param string|zend_db_select $sql  * @throws zend_db_adapter_exception  */ protected function _checkddltransaction($sql) {     if (is_string($sql) && $this->gettransactionlevel() > 0) {         $startsql = strtolower(substr(ltrim($sql), 0, 3));         if (in_array($startsql, $this->_ddlroutines)) {             trigger_error(varien_db_adapter_interface::error_ddl_message, e_user_error);         }     } }
结论是mysql不支持ddl transaction。
因此在app/code/local/{companyname}/{modulename}/setup/helper.php里重写createentitytable方法
{         ...           /**          * remove transaction code due to issues with errors.          */         //$connection->begintransaction();         try {              foreach ($tables as $tablename => $table) {                 $connection->createtable($table);             }             $connection->commit();        } catch (exception $e) {            //$connection->rollback();            throw mage::exception('mage_eav', mage::helper('eav')->__('can\'t create table: %s', $tablename));        }     } }
然后问题解决。
setup脚本剖析
让我们一行一行的解释。首先
$installer = $this;
每个安装脚本都是从setresource类开始执行的(就是我们上面创建的)。这意味着脚本中的$this引用是这个类实例化的引用。如果不是必须,core系统里大部分安装脚本都是把$this命名未installer,此处我们也是这样。
接下来我们看到了两个方法
$installer->startsetup();
//...
$installer->endsetup();
如果查看mage_core_model_resource_setup类(在目录app/code/core/mage/core/resource/setup.php),你可以看到如下的内容
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;
}
最后我们执行
$installer->run(...);
这个接受了一个包含创建数据库的sql。你可定义任意的查询,通过分号隔开就好。同时,也要注意
$installer->gettable('weblog/blogpost')
gettable方法容许我们把magento model uri传入,然后得到它的表名。如果不是必要,就用次方法执行。mage_core_model_resource_setup类包含了很多有用的helper方法。最有效的学习是研究magento core的installer scripts。
module升级
上面讲述了如何初始化数据表,但是如何改变现有墨香的结构呢?magento的setup resources支持一个简单的版本策略,可以让我们自动的执行脚本来升级我们的模块。
一旦magento执行一个安装脚本后,它就不会再次执行另外一个安装脚本。这个时候,我们应该创建一个升级脚本。升级脚本跟安装脚本非常类似,只有有些关键处不一样。
作为开始,我们在下列位置创建一个脚本,
xstarx/weblog/sql/weblog_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();
升级脚本和安装脚本在同一个目录,但是略有不同。首先,文件名要包含upgrade。其次,要有两个版本号,并用“-”分隔。第一个是升级的源版本,第二个是升级的目标版本。
清除cache后,重新加载页面,但这个时候脚本并没有执行。我们需要更新config.xml里面的版本信息来触发升级
0.2.0
写入新的版本号后,如果清除缓存,加载网站,就可以看到输出了。这个时候还有一个关键点需要注意,所以先不慌做这一步。我们在同样的目录创建另外一个文件
xstarx/weblog/sql/weblog_setup/mysql4-upgrade-0.1.0-0.1.5.php
echo 'testing our upgrade script (mysql4-upgrade-0.1.0-0.1.5.php) and not halting execution
';
这个时候再清除缓存,加载页面,可以看到两个信息。当magento发现版本号信息变更后,他会执行所有可执行的脚本来更新模块。尽管我们从没有创建0.1.5版本,但是magento会看到升级脚本,然后尝试执行。脚本一般按照从低到高的顺序执行。下面的数据会说明这个
mysql> select * from core_resource where code = 'weblog_setup'; +--------------+---------+
| code         | version | +--------------+---------+
| weblog_setup | 0.1.5   |  +--------------+---------+
1 row in set (0.00 sec)
我们看到数据表里的版本是1.5。这是因为我们从1.0到1.5升级,但是没有执行1.0到2.0的升级。好了,说明了这个关键问题后,我们言归正传。回到脚本上来,先修改升级脚本0.1.0-0.2.0
$installer = $this;
$installer->startsetup();
$installer->run(
alter table `{$installer->gettable('weblog/blogpost')}`
change post post text not null; );
$installer->endsetup();
die(you'll see why this is here in a second);
刷新页面,但是什么也不会发生。升级脚本为什么没有执行?
1.     weblog_setup resource是版本0.1.0
2.     我们要升级模块到0.2.0
3.     magento看到升级模块,有两个脚本要执行,0.1.0-0.1.5 和0.1.0-0.2.0
4.     magento载入队列,然后执行
5.     magento执行0.1.0到0.1.5的脚本
6.     weblog_setup resource现在是0.1.5了
7.     magento执行0.1.0到0.2.0的脚本,执行停止
8.     在下一个页面加载的时候,magento看到了weblog_set在版本0.1.5,但是并没有看到任何从0.1.5开始执行的脚本(之前的都是0.1.0开始)
正确的方式如下,重新命名文件
mysql4-upgrade-0.1.0-0.1.5.php #this goes from 0.1.0 to 0.1.5
mysql4-upgrade-0.1.5-0.2.0.php #this goes 0.1.5 to 0.2.0
magento是能够完成一次加载两次升级的。你可以清除core_resource表信息,来完成最后的test
update core_resource set version = '0.1.0' where code = 'weblog_setup';
magento是根据配置文件来执行升级的,所以在协同开发时要注意脚本的添加。
http://www.bkjia.com/phpjc/477854.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/477854.htmltecharticle在任何一个快速迭代的工程下,如何保证开发和生产(现网)数据库同步是一个很头疼的事情。magento提供了一个创建资源迁移版本的系统,...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product