本篇文章更新于2019年6月1号,主要讲述的是magento 1的数据库的安装代码安装教程,有不懂的小伙伴可以看看。
作为magento的开发人员有时候我们业务上的需求,要求magento开发者开发一个功能模块,这个时候就有可能需要创建
数据库表,创字段名等等。当然你创建数据库表的时候可以直接在可视化客户端数据库里面直接创建表和字段,作为一名
专业的开发人员,这一样做出来的Magento插件不是完美的,所以为了让插件安装更加顺利,就需要magento代码进行创建
数据库的,这样开发的插件可移植性会很好,直接安装就能完成了。
好了,多余的话不多少,接下来就让我们看看如何使用magento的代码进行数据库表的安装吧。
第一步:首先我们要创建好自己模块,在自己安装的magento目录app/code/local目录下面创建Sky8g/Hello两个层级目录
1 | 你的magento安装目录/app/code/local/Sky8g/Hello |
第二步:在Hello目录下分别创建如下目录:Block ,controllers , etc , Helper , Model , sql最好都创建一下。
1 2 3 4 5 6 | 你的magento安装目录/app/code/local/Sky8g/Hello/Block 你的magento安装目录/app/code/local/Sky8g/Hello/controller 你的magento安装目录/app/code/local/Sky8g/Hello/etc 你的magento安装目录/app/code/local/Sky8g/Hello/Helper 你的magento安装目录/app/code/local/Sky8g/Hello/Model 你的magento安装目录/app/code/local/Sky8g/Hello/sql |
注:注意大小写这个非常重要,不然程序会运行不了。
第三步:在etc目录下面创建文件config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | <?xml version="1.0"?> <!-- /** * SKY8G VIPMembership integration. * * @author (SKY8G.com) * @copyright Copyright (c) 2019 SKY8G * @license https://www.sky8g.com Open Software License (OSL 3.0) */ --> <config> <modules> <Sky8g_Hello> <version>1.1.1</version> </Sky8g_Hello> </modules> <frontend> <routers> <vipmembership> <use>standard</use> <args> <module>Sky8g_Hello</module> <frontName>membership</frontName> </args> </vipmembership> </routers> </frontend> <global> <helpers> <vipmembership><class>Sky8g_Hello_Helper</class></vipmembership> </helpers> <resources> <vipmembership_setup> <setup> <module>Sky8g_Hello</module> <class>Sky8g_Hello_Model_Mysql4_Setup</class> </setup> <connection> <use>core_setup</use> </connection> </vipmembership_setup> <vipmembership_write> <connection> <use>core_write</use> </connection> </vipmembership_write> <vipmembership_read> <connection> <use>core_read</use> </connection> </vipmembership_read> </resources> </global> </config> |
第四步:在sql目录下创建目录vipmembership_setup 在创建好的vipmembership_setup这个目录下再次创建文件mysql4-install-1.1.0.php即可
1 | 你的magento安装目录/app/code/local/Sky8g/Hello/sql/vipmembership_setup/mysql4-install-1.1.0.php |
第五步:在 文件mysql4-install-1.1.0.php里面把下面的代码放到文件里面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <?php /** * SKY8G VIPMembership integration. * * @author (SKY8G.com) * @copyright Copyright (c) 2019 * @license https://www.SKY8G.com Open Software License (OSL 3.0) */ $installer = $this; $installer->startSetup(); Mage::helper('vipmembership/mysql4_install')->prepareForDb(); Mage::helper('vipmembership/mysql4_install')->attemptQuery($installer, " CREATE TABLE IF NOT EXISTS `{$this->getTable('vipmembership_order')}` ( `id` int(10) unsigned NOT NULL auto_increment, `state` varchar(32) NULL, `create_at` datetime NULL, `expiration_date` datetime NULL, `customer_email` varchar(255) NOT NULL default '', `customer_id` int(10) unsigned NULL, `increment_id` varchar(50) NULL, `total` decimal (12,4) DEFAULT '0.0000' NULL, `transaction_info` text, `apply_rule_ids` varchar(225) NULL, `comment` text, PRIMARY KEY (`order_id`), KEY `create_at` (`create_at`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; "); Mage::helper('vipmembership/mysql4_install')->createInstallNotice("SKY8G Extensions was installed successfully.", "SKY8G Extensions has been installed successfully. Go to the system configuration section of your Magento admin to configure SKY8G Extensions and get it up and running."); $installer->endSetup(); |
第六步:需要创建Helper目录下创建目录Mysql4在创建好的目录再次创建Install.php文件如下
1 | 你的magento安装目录/app/code/local/Sky8g/Hello/Helper/Mysql4/Install.php |
第七步:在创建好的文件Install.php里面放入下面代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | <?php /** * SKY8G VIPMembership integration. * * @author (SKY8G.com) * @copyright Copyright (c) 2019 * @license https://www.SKY8G.com Open Software License (OSL 3.0) */ class Sky8g_Hello_Helper_Mysql4_Install extends Mage_Core_Helper_Abstract { protected $ex_stack = array (); protected $_setup = null; /** * alter table for each column update and ignore duplicate column errors * This is used since "if column not exists" function does not exist * for MYSQL * * @param unknown_type $installer * @param string $table_name * @param array $columns * @return Sky8g_Hello_Helper_Mysql4_Install */ public function addColumns(&$installer, $table_name, $columns) { foreach($columns as $column) { $sql = "ALTER TABLE {$table_name} ADD COLUMN({$column});"; // run SQL and ignore any errors including (Duplicate column errors) try { $installer->run($sql); } catch(Exception $ex) { $this->addInstallProblem($ex); } } return $this; } /** * Attempt to add a foreign key constraint on two tables * * @param unknown_type $installer * @param string $table1_name * @param string $column1 * @param string $table2_name * @param string $column2=null (uses column1 if null) * @param string $on_delete='CASCADE' * @param string $on_update='NO ACTION' * @return Sky8g_Hello_Helper_Mysql4_Install */ public function addFKey(&$installer, $key_name, $table1_name, $column1, $table2_name, $column2=null, $on_delete='NO ACTION', $on_update='NO ACTION') { try { if(empty($column2)) { $column2 = $column1; } $installer->getConnection() ->addConstraint( $key_name, $table1_name, $column1, $table2_name, $column2, $on_delete, $on_update ); } catch(Exception $ex) { $this->addInstallProblem($ex); } return $this; } /** * Adds an exception problem to the stack of problems that may * have occured during installation. * Ignores duplicate column name errors; ignore if the msg starts with "SQLSTATE[42S21]: Column already exists" * @param Exception $ex */ public function addInstallProblem(Exception $ex) { if (strpos($ex->getMessage (), "SQLSTATE[42S21]: Column already exists") !== false) return $this; if (strpos($ex->getMessage (), "SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP") !== false && strpos($ex->getMessage (), "check that column/key exists") !== false ) return $this; $this->ex_stack [] = $ex; return $this; } /** * Returns true if any problems occured after installation * @return boolean */ public function hasProblems() { return sizeof($this->ex_stack ) > 0; } /** * Returns a string of problems that occured after any installation scripts were run through this helper * @return string message to display to the user */ public function getProblemsString() { $msg = $this->__("The following errors occured while trying to install the module."); $msg .= "\n<br>"; foreach($this->ex_stack as $ex_i => $ex) { $msg .= "<b>#{$ex_i}: </b>"; if(Mage::getIsDeveloperMode()) { $msg .= nl2br($ex); } else { $msg .= $ex->getMessage (); } $msg .= "\n<br>"; } $msg .= "\n<br>"; $msg .= $this->__("If any of these problems were unexpected, I recommend that you contact the module support team to avoid problems in the future."); return $msg; } /** * Clears any insall problems (exceptions) that were in the stack */ public function clearProblems() { $this->ex_stack = array (); return $this; } /** * alter table for each column update and ignore duplicate column errors * This is used since "if column not exists" function does not exist * for MYSQL * * @param unknown_type $installer * @param string $table_name * @param array $columns * @return Sky8g_Hello_Helper_Mysql4_Install */ public function dropColumns(&$installer, $table_name, $columns) { foreach($columns as $column) { $sql = "ALTER TABLE {$table_name} DROP COLUMN {$column} ;"; // run SQL and ignore any errors including (Duplicate column errors) try { $installer->run($sql); } catch(Exception $ex) { $this->addInstallProblem($ex); } } return $this; } /** * Runs a SQL query using the install resource provided and * remembers any errors that occur. * * @param unknown_type $installer * @param string $sql * @return Sky8g_Hello_Helper_Mysql4_Install */ public function attemptQuery(&$installer, $sql) { try { $installer->run($sql); } catch(Exception $ex) { $this->addInstallProblem($ex); } return $this; } /** * Creates an installation message notice in the backend. * @param string $msg_title * @param string $msg_desc * @param string $url=null if null default URL is used. */ public function createInstallNotice($msg_title, $msg_desc, $url = null, $severity = null) { $message = Mage::getModel('adminnotification/inbox'); $message->setDateAdded(date("c", time () )); if ($url == null) { $url = "https://www.SKY8G.com"; } if ($severity === null) { $severity = Mage_AdminNotification_Model_Inbox::SEVERITY_NOTICE; } // If problems occured increase severity and append logged messages. if (Mage::helper('vipmembership/mysql4_install' )->hasProblems ()) { $severity = Mage_AdminNotification_Model_Inbox::SEVERITY_MINOR; $msg_title .= " Problems may have occured during installation."; $msg_desc .= " " . Mage::helper('vipmembership/mysql4_install' )->getProblemsString (); Mage::helper('vipmembership/mysql4_install' )->clearProblems (); } $message->setTitle($msg_title); $message->setDescription($msg_desc); $message->setUrl($url); $message->setSeverity($severity); $message->save (); return $this; } /** * Add an EAV entity attribute to the database. * * @param string $entity_type entity type (catalog_product, order, order_item, etc) * @param string $attribute_code attribute code * @param array $data eav attribute data */ public function addAttribute($entity_type, $attribute_code, $data) { try { $this->_getSetupSingleton ()->addAttribute($entity_type, $attribute_code, $data); } catch(Exception $ex) { $this->addInstallProblem($ex); } return $this; } /** * Clears cache and prepares anything that needs to generally happen before running DB install scripts. */ public function prepareForDb() { try { Mage::app()->getCacheInstance()->flush(); } catch(Exception $ex) { $this->addInstallProblem("Problem clearing cache:". $ex); } return $this; } /** * @return Mage_Eav_Model_Entity_Setup */ protected function _getSetupSingleton() { if ($this->_setup == null) { $this->_setup = new Mage_Eav_Model_Entity_Setup('core_setup'); } return $this->_setup; } } |
第八步:在的magento安装目录/app/etc/modules目录下创建Sky8g_Hello.xml,内容如下
1 2 3 4 5 6 7 8 9 10 | <?xml version="1.0"?> <config> <modules> <Sky8g_Hello> <active>true</active> <codePool>local</codePool> </Sky8g_Hello> </modules> </config> |
保存上传,访问访问你的网站即可,然后你在查看你的数据库是否安装了表vipmembership_order,感谢你的浏览,如果有疑问
请留言,我将尽快回复你。