1.新建文件app\etc\modules\Eachgame_Helloorm.xml,内容如下。告知Magento系统该模块的存在,其中active表示是否启用,codePool告知模块在local目录下。
-
-
<?xml version='1.0'
-
<config>
-
<modules>
-
<Eachgame_Helloorm>
-
<active>true</active>
-
<codePool>local</codePool>
-
</Eachgame_Helloorm>
-
</modules>
-
</config>
2.在app\code\local\Eachgame\Helloorm\下新建几个目录,分别是Block、controllers、etc、Helper、Model、sql目录。
3.在数据库中新建对应的表,在phpmyadmin中magento数据库中输入如下内容。生成一个表并插入一条数据
-
-
CREATE TABLE `orm_contents` (
-
`orm_id` int(11) NOT NULL auto_increment,
-
`title` text,
-
`content` text,
-
`date` datetime default NULL,
-
`timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,
-
PRIMARY KEY (`orm_id`)
-
);
-
INSERT INTO `orm_contents` VALUES (1,'My New Title','This is a orm content','2010-03-03 00:00:00','2010-03-06 23:12:30');
4.新建app\code\local\Eachgame\Helloorm\etc.xml文件,内容如下。其中modules记录版本信息,routers记录路由信息,如下配置表示http://www.shop.com/helloorm/会用Eachgame_Helloorm模块进行处理。Models下面指定模型对应的类,以及指定对应的资源模型为helloorm_mysql4。然后定资源模型helloorm_mysql4的相关信息:对应到类,对应的数据表。Resources指定的是操作数据库的类型,使用的是core_read和core_write。helloorm_setup表示使用Eachgame_Helloorm_Model_Resource_Mysql4_Setup类进行安装。
-
-
<?xml version='1.0'
-
<config>
-
<modules>
-
<Eachgame_Helloorm>
-
<version>0.0.2</version>
-
</Eachgame_Helloorm>
-
</modules>
-
<frontend>
-
<routers>
-
<helloorm>
-
<use>standard</use>
-
<args>
-
<module>Eachgame_Helloorm</module>
-
<frontName>helloorm</frontName>
-
</args>
-
</helloorm>
-
</routers>
-
</frontend>
-
<global>
-
<models>
-
<helloorm>
-
<class>Eachgame_Helloorm_Model</class>
-
<resourceModel>helloorm_mysql4</resourceModel>
-
</helloorm>
-
<helloorm_mysql4>
-
<class>Eachgame_Helloorm_Model_Mysql4</class>
-
<entities>
-
<ormcontent>
-
<table>orm_contents</table>
-
</ormcontent>
-
</entities>
-
</helloorm_mysql4>
-
</models>
-
<resources>
-
<helloorm_write>
-
<connection>
-
<use>core_write</use>
-
</connection>
-
</helloorm_write>
-
<helloorm_read>
-
<connection>
-
<use>core_read</use>
-
</connection>
-
</helloorm_read>
-
<helloorm_setup>
-
<setup>
-
<module>Eachgame_Helloorm</module>
-
<class>Eachgame_Helloorm_Model_Resource_Mysql4_Setup</class>
-
</setup>
-
<connection>
-
<use>core_setup</use>
-
</connection>
-
</helloorm_setup>
-
</resources>
-
</global>
-
</config>
5.新建app\code\local\Eachgame\Helloorm\controllers\IndexController.php,内容如下。
-
-
class Eachgame_Helloorm_IndexController extends Mage_Core_Controller_Front_Action
-
{
-
public function indexAction() {
-
//echo 'Hello orm!';
-
$orm = Mage::getModel('helloorm/ormcontent');
-
echo get_class($orm);
-
-
}
-
-
public function loadAction() {
-
//echo 'Hello orm!';
-
$params = $this->getRequest()->getParams();
-
$orm = Mage::getModel('helloorm/ormcontent');
-
echo 'begin to read data of id:' . $params['id'];
-
Mage::log('test log');
-
$orm->load($params['id']);
-
$data = $orm->getData();
-
var_dump($data);
-
}
-
-
public function createAction() {
-
$orm = Mage::getModel('helloorm/ormcontent');
-
echo 'begin to create new data';
-
$orm->setTitle('new orm title');
-
$orm->setContent('new orm content');
-
$orm->save();
-
echo 'create new data success';
-
}
-
-
public function updateAction() {
-
$params = $this->getRequest()->getParams();
-
$orm = Mage::getModel('helloorm/ormcontent');
-
echo 'begin to update data of id:' . $params['id'];
-
$orm->load($params['id']);
-
$orm->setTitle('new orm update title');
-
$orm->save();
-
echo 'update new data success of id:'.$params['id'];
-
}
-
-
public function deleteAction() {
-
//echo 'Hello orm!';
-
$params = $this->getRequest()->getParams();
-
$orm = Mage::getModel('helloorm/ormcontent');
-
echo 'begin to read data of id:' . $params['id'];
-
$orm->load($params['id']);
-
$orm->delete();
-
echo 'delete new data success of id:'.$params['id'];
-
}
-
-
public function loadAllAction() {
-
//echo 'Hello orm!';
-
$ormData = Mage::getModel('helloorm/ormcontent')->getCollection();
-
foreach($ormData as $key=>$value)
-
{
-
echo '<h3>' . $value->getTitle() . '</h3>';
-
echo nl2br($value->getContent());
-
}
-
}
-
}
6.新建app\code\local\Eachgame\Helloorm\Model\Ormcontent.php,添加如下内容
-
-
class Eachgame_Helloorm_Model_Ormcontent extends Mage_Core_Model_Abstract
-
{
-
protected function _construct()
-
{
-
$this->_init('helloorm/ormcontent');
-
}
-
}
7.新建app\code\local\Eachgame\Helloorm\Model\Mysql4\Ormcontent.php,添加如下内容
-
-
class Eachgame_Helloorm_Model_Mysql4_Ormcontent extends Mage_Core_Model_Mysql4_Abstract
-
{
-
protected function _construct()
-
{
-
$this->_init('helloorm/ormcontent', 'orm_id');
-
}
-
}
8.在浏览器输入http://www.shop.com/magento1411/helloorm/可以显示Eachgame_Helloorm_Model_Ormcontent。输入http://www.shop.com/magento1411/helloorm/index/load/id/1会显示初始插入到数据库中的记录内容。http://www.shop.com/magento1411/helloorm/index/create会在orm_contents表中插入一条数据。http://www.shop.com/magento1411/helloorm/index/update/id/2会更新数据表orm_contents中id为2的数据。
http://www.shop.com/magento1411/helloorm/index/delete/id/2会删除数据表orm_contents中id为2的数据。
9.如果需要显示多条记录,也就是数据集合,则新建app\code\local\Eachgame\Helloorm\Model\Mysql4\Ormcontent\Collection.php,添加如下内容
-
-
class Eachgame_Helloorm_Model_Mysql4_Ormcontent_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
-
{
-
protected function _construct()
-
{
-
$this->_init('helloorm/ormcontent');
-
}
-
}
10.在浏览器输入http://www.shop.com/magento1411/helloorm/index/loadAll,浏览器显示数据表orm_contents中所有的数据。
11.新建app\code\local\Eachgame\Helloorm1\Model\Resource\Mysql4\Setup.php文件
-
-
class Eachgame_Helloorm_Model_Resource_Mysql4_Setup extends Mage_Core_Model_Resource_Setup
-
{
-
}
12.模块安装:新建app\code\local\Eachgame\Helloorm\sql\helloorm_setup\mysql4-install-0.0.1.php文件,添加如下内容。Eachgame_Helloorm_Model_Resource_Mysql4_Setup类会自动调用该文件的内容并在数据库中生成orm_contents表。注意定期清除core_resource中helloorm的版本信息。
-
-
$installer = $this;
-
/* @var $installer Mage_Core_Model_Resource_Setup */
-
-
$installer->startSetup();
-
-
$installer->run('
-
-
DROP TABLE IF EXISTS {$this->getTable('orm_contents')};
-
CREATE TABLE {$this->getTable('orm_contents')} (
-
`orm_id` int(11) NOT NULL auto_increment,
-
`title` text,
-
`content` text,
-
`date` datetime default NULL,
-
`timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,
-
PRIMARY KEY (`orm_id`)
-
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-
-
');
-
-
$installer->endSetup();
13.在浏览器中输入:http://www.shop.com/helloorm/,在phpmyadmin中查看orm_contents表已经被生成。在core_resource表中helloorm的信息为0.0.1。
14.模块升级:新建app\code\local\Eachgame\Helloorm\sql\mysql4-upgrade-0.0.1-0.0.2.php,添加如下内容。
-
-
$installer = $this;
-
/* @var $installer Mage_Core_Model_Resource_Setup */
-
-
$installer->startSetup();
-
-
$installer->run('
-
-
alter table {$this->getTable('orm_contents')}
-
CHANGE title title varchar(255) not null;
-
-
');
-
-
$installer->endSetup();
15.修改app\code\local\Eachgame\Helloorm\etc\config.xml中的modules版本信息为0.0.2。在浏览器中输入:http://www.shop.com/helloorm/,在phpmyadmin中查看orm_contents表title属性已经变更。在core_resource表中helloorm的信息为0.0.2。