magento ----使用magento内部机制建立数据库表

本文档详细介绍了如何从零开始创建一个 Magento 模块,包括模块的基本结构、配置文件的设置、数据库表的创建等步骤。

1

首先建立一个模块:

app/code/community/Terry/Zhao

下面建立

Block,controllers,Model,etc,sql,Helper文件夹

2

app/etc/modules下面

建立配置文件

Terry_zhao.xml

内容:

<?xml version="1.0" ?>

<config>

<modules>

<Terry_Zhao>

<active>true</active>

<codePool>community</codePool>

</Terry_Zhao>

</modules>

</config>

3

app/code/community/Terry/Zhao/etc

建立配置文件config.xml

<?xml version="1.0" ?>

<config>

<modules>

<Terry_Zhao>

<version>1.10</version>

</Terry_Zhao>

</modules>

<frontend>

<routers>

<zhao>

<use>standard</use>

<args>

<module>Terry_Zhao</module>

<frontName>zhao</frontName>

</args>

</zhao>

</routers>

</frontend>

<global>

<models>

<zhao>

<class>Terry_Zhao_Model</class>

<resourceModel>zhao_mysql4</resourceModel>

</zhao>

<zhao_mysql4>

<class>Terry_Zhao_Model_Mysql4</class>

<entities>

<zhao>

<table>zhao</table>

</zhao>

<zhao_store>

<table>zhao_store</table>

</zhao_store>

</entities>

</zhao_mysql4>

</models>

<resources>

<zhao_setup>

<setup>

<module>Terry_Zhao</module>

</setup>

<connection>

<use>core_setup</use>

</connection>

</zhao_setup>

<zhao_write>

<connection>

<use>core_write</use>

</connection>

</zhao_write>

<zhao_read>

<connection>

<use>core_read</use>

</connection>

</zhao_read>

</resources>

<blocks>

<profile>

<class>Terry_Zhao_Block</class>

</profile>

</blocks>

<helpers>

<profile>

<class>Terry_Zhao_Helper</class>

</profile>

</helpers>

</global>

</config>

4
在目录
app/code/community/Terry/Zhao/sql/zhao_setup
下面建立文件

mysql4-install-0.1.0.php

内容:

<?php

$installer = $this;

$installer->startSetup();

$installer->run("

DROP TABLE IF EXISTS `{$this->getTable('zhao/zhao')}`;

CREATE TABLE {$this->getTable('zhao/zhao')} (

`profile_id` smallint(6) NOT NULL AUTO_INCREMENT,

`category_id` int(11) NOT NULL,

`thumbnail` varchar(255) NOT NULL,

`thumbnail_position` int(11) NOT NULL,

`picture` varchar(255) NOT NULL,

`file` varchar(255) NOT NULL,

`content_heading` varchar(255) NOT NULL DEFAULT '',

`content` mediumtext,

`meta_keywords` text NOT NULL,

`meta_description` text NOT NULL,

`creation_time` datetime DEFAULT NULL,

`update_time` datetime DEFAULT NULL,

`is_active` tinyint(1) NOT NULL DEFAULT '1',

PRIMARY KEY (`profile_id`),

KEY `identifier` (`content_heading`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `{$this->getTable('zhao/zhao_store')}`;

CREATE TABLE {$this->getTable('zhao/zhao_store')} (

`profile_id` smallint(6) NOT NULL,

`store_id` smallint(5) unsigned NOT NULL,

PRIMARY KEY (`profile_id`,`store_id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='CMS Profiles to Stores';

");

$installer->endSetup();

OK。只要刷新下缓存,或者访问这个模块后,查看数据库,就会发现建立了2个数据表

zhao

zhap_store

两个数据库表。

so easy!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值