install.php和upgrade.php

本文整理于网络,仅供阅读参考,如有不妥之处,敬请批评指正!如果您想加入微擎社区版技术大牛微信群和QQ群,请联系微信: ccccyyyy4444 或者 QQ:155120699 

模块安装文件install.php和升级文件upgrade.php的说明

关于这两个文件的几点说明:

  • 模块安装脚本是指install.php,该文件是manifest.xml文件内节点<install><![CDATA[]]></install>的分离;
  • 模块升级脚本是指upgrade.php,该文件是manifest.xml文件内节点<upgrade><![CDATA[]]></upgrade>的分离;
  • 若新安装或升级版本需要执行某些脚本,则在对应文件内写相应脚本即可;
  • 新安装或新版本需要执行的脚本,需在对应文件代码的基础上继续增加;
  • 升级脚本中涉及到新加表,创建语句要有IF NOT EXISTS,否则后续添加其他脚本时会报错;
  • 表前缀问题:统一写成‘ims_’,系统内程序在执行安装或升级脚本时自动会替换成客户的表前缀;

举例说明

1、模块A发布,版本1.0.0,模块功能需要表 table_a(在install.php内添加创建语句);
manifest.xml文件中关于install节点和upgrade节点可以写成如下形式:

...
...
    <install><![CDATA[CREATE TABLE IF NOT EXISTS `ims_table_a` (
		`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
		`uniacid` int(10) NOT NULL,
		`title` varchar(100) NOT NULL,
		`content` mediumtext NOT NULL,
		PRIMARY KEY (`id`),
		KEY `uniacid` (`uniacid`),
		) ENGINE=InnoDB  DEFAULT CHARSET=utf8;]]></install>
    <upgrade><![CDATA[]]></upgrade>
...
...

...
...
    <install><![CDATA[install.php]]></install>
    <upgrade><![CDATA[]]></upgrade>
...
...

install.php文件如下:

pdo_run("CREATE TABLE IF NOT EXISTS `ims_table_a` (
		`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
		`uniacid` int(10) NOT NULL,
		`title` varchar(100) NOT NULL,
		`content` mediumtext NOT NULL,
		PRIMARY KEY (`id`),
		KEY `uniacid` (`uniacid`),
		) ENGINE=InnoDB  DEFAULT CHARSET=utf8");

2、模块A发布新版本1.0.1,新添加功能需要在表table_a添加category_id字段及新表 table_b,则需在install.php中加上字段category_id和新表table_b的创建语句,在upgrade.php中加上字段category_id的执行sql和新表table_b的执行sql,具体可参考如下:
manifest.xml文件中关于install节点和upgrade节点可以写成如下形式:

...
...
    <install><![CDATA[CREATE TABLE IF NOT EXISTS `ims_table_a` (
		`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
		`uniacid` int(10) NOT NULL,
		`title` varchar(100) NOT NULL,
		`content` mediumtext NOT NULL,
		`category_id` int(10) DEFAULT NULL,
		PRIMARY KEY (`id`),
		KEY `uniacid` (`uniacid`),
		) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
		CREATE TABLE IF NOT EXISTS `ims_table_b` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  `uniacid` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `title` (`title`),
  KEY `uniacid` (`uniacid`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;]]></install>
    <upgrade><![CDATA[CREATE TABLE IF NOT EXISTS `ims_table_b` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  `uniacid` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `title` (`title`),
  KEY `uniacid` (`uniacid`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;]]></upgrade>
...
...

...
...
    <install><![CDATA[install.php]]></install>
    <upgrade><![CDATA[upgrade.php]]></upgrade>
...
...

install.php文件如下:

pdo_run("CREATE TABLE IF NOT EXISTS `ims_table_a` (
		`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
		`uniacid` int(10) NOT NULL,
		`title` varchar(100) NOT NULL,
		`content` mediumtext NOT NULL,
		`category_id` int(10) DEFAULT NULL,
		PRIMARY KEY (`id`),
		KEY `uniacid` (`uniacid`),
		) ENGINE=InnoDB  DEFAULT CHARSET=utf8");
pdo_run("CREATE TABLE IF NOT EXISTS `ims_table_b` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  `uniacid` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `title` (`title`),
  KEY `uniacid` (`uniacid`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;");

upgrade.php文件如下:

pdo_run("ALTER TABLE `ims_table_a` ADD `category_id` int(10) DEFAULT NULL; ");
pdo_run("CREATE TABLE IF NOT EXISTS `ims_table_b` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  `uniacid` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `title` (`title`),
  KEY `uniacid` (`uniacid`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;");

3、模块A发布新版本1.0.2,新添加功能需要在表table_b添加displayorder字段,则需要在upgrade.php继续添加该字段的sql语句(manifest.xml文件修改方法参见第二步);
install.php文件如下:

pdo_run("CREATE TABLE IF NOT EXISTS `ims_table_a` (
		`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
		`uniacid` int(10) NOT NULL,
		`title` varchar(100) NOT NULL,
		`content` mediumtext NOT NULL,
		`category_id` int(10) DEFAULT NULL,
		PRIMARY KEY (`id`),
		KEY `uniacid` (`uniacid`),
		) ENGINE=InnoDB  DEFAULT CHARSET=utf8");
pdo_run("CREATE TABLE IF NOT EXISTS `ims_table_b` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  `uniacid` int(10) unsigned NOT NULL,
  `displayorder` int(10) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `title` (`title`),
  KEY `uniacid` (`uniacid`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;");

upgrade.php文件如下:

pdo_run("ALTER TABLE `ims_table_a` ADD `category_id` int(10) DEFAULT NULL; ");
pdo_run("CREATE TABLE IF NOT EXISTS `ims_table_b` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  `uniacid` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `title` (`title`),
  KEY `uniacid` (`uniacid`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;");
pdo_run("ALTER TABLE `ims_table_b` ADD `displayorder` int(10) DEFAULT NULL;");
本页目录
  1. 模块安装文件install.php和升级文件upgrade.php的说明
  2. 举例说明
文档统计
浏览次数:175752次
编辑次数:210次历史版本
最近更新:2023-01-16 16:16:34

如果看不懂微擎社区版二次开发手册或者遇到问题,请联系微信: ccccyyyy4444 或者 QQ:155120699 ,如果我们有空闲时间,可以免费为您答疑解惑。