Magento二次开发4-最简单的数据库安装和升级


1.新建文件app\etc\modules\Eachgame_Helloorm.xml,内容如下。告知Magento系统该模块的存在,其中active表示是否启用,codePool告知模块在local目录下。

  1.   
  2.  <?xml version='1.0'?>
  3.  <config>
  4.      <modules>
  5.          <Eachgame_Helloorm>
  6.              <active>true</active>
  7.              <codePool>local</codePool>
  8.          </Eachgame_Helloorm>
  9.      </modules>
  10.  </config>


2.在app\code\local\Eachgame\Helloorm\下新建几个目录,分别是Block、controllers、etc、Helper、Model、sql目录。

3.在数据库中新建对应的表,在phpmyadmin中magento数据库中输入如下内容。生成一个表并插入一条数据

  1.   
  2.  CREATE TABLE `orm_contents` (
  3.    `orm_id` int(11) NOT NULL auto_increment,
  4.    `title` text,
  5.    `content` text,
  6.    `date` datetime default NULL,
  7.    `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,
  8.    PRIMARY KEY  (`orm_id`)
  9.  );
  10.  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类进行安装。

  1.   
  2.  <?xml version='1.0'?>
  3.  <config>
  4.      <modules>
  5.          <Eachgame_Helloorm>
  6.              <version>0.0.2</version>
  7.          </Eachgame_Helloorm>
  8.      </modules>
  9.      <frontend>
  10.          <routers>
  11.              <helloorm>
  12.                  <use>standard</use>
  13.                  <args>
  14.                      <module>Eachgame_Helloorm</module>
  15.                      <frontName>helloorm</frontName>
  16.                  </args>
  17.              </helloorm>
  18.          </routers>
  19.      </frontend>
  20.      <global>
  21.          <models>
  22.              <helloorm>
  23.                  <class>Eachgame_Helloorm_Model</class>
  24.                  <resourceModel>helloorm_mysql4</resourceModel>
  25.              </helloorm>
  26.              <helloorm_mysql4>
  27.                  <class>Eachgame_Helloorm_Model_Mysql4</class>
  28.                  <entities>
  29.                      <ormcontent>
  30.                          <table>orm_contents</table>
  31.                      </ormcontent>
  32.                  </entities>
  33.              </helloorm_mysql4>
  34.          </models>
  35.          <resources>
  36.              <helloorm_write>
  37.                  <connection>
  38.                      <use>core_write</use>
  39.                  </connection>
  40.              </helloorm_write>
  41.              <helloorm_read>
  42.                  <connection>
  43.                      <use>core_read</use>
  44.                  </connection>
  45.              </helloorm_read>
  46.              <helloorm_setup>
  47.                  <setup>
  48.                      <module>Eachgame_Helloorm</module>
  49.                      <class>Eachgame_Helloorm_Model_Resource_Mysql4_Setup</class>
  50.                  </setup>
  51.                  <connection>
  52.                      <use>core_setup</use>
  53.                  </connection>
  54.              </helloorm_setup>
  55.          </resources>        
  56.      </global>
  57.  </config>


5.新建app\code\local\Eachgame\Helloorm\controllers\IndexController.php,内容如下。

  1.   
  2.  class Eachgame_Helloorm_IndexController extends Mage_Core_Controller_Front_Action 
  3.  {        
  4.      public function indexAction() {
  5.          //echo 'Hello orm!';
  6.          $orm = Mage::getModel('helloorm/ormcontent');
  7.          echo get_class($orm);
  8.   
  9.      }
  10.   
  11.      public function loadAction() {
  12.          //echo 'Hello orm!';
  13.          $params = $this->getRequest()->getParams();
  14.          $orm = Mage::getModel('helloorm/ormcontent');
  15.          echo 'begin to read data of id:' . $params['id'];
  16.          Mage::log('test log');
  17.          $orm->load($params['id']);
  18.          $data = $orm->getData();
  19.          var_dump($data);
  20.      }
  21.   
  22.      public function createAction() {
  23.          $orm = Mage::getModel('helloorm/ormcontent');
  24.          echo 'begin to create new data';
  25.          $orm->setTitle('new orm title');
  26.          $orm->setContent('new orm content');
  27.          $orm->save();
  28.          echo 'create new data success';
  29.      }
  30.   
  31.      public function updateAction() {
  32.          $params = $this->getRequest()->getParams();
  33.          $orm = Mage::getModel('helloorm/ormcontent');
  34.          echo 'begin to update data of id:' . $params['id'];
  35.          $orm->load($params['id']);
  36.          $orm->setTitle('new orm update title');
  37.          $orm->save();
  38.          echo 'update new data success of id:'.$params['id'];
  39.      }
  40.      
  41.      public function deleteAction() {
  42.          //echo 'Hello orm!';
  43.          $params = $this->getRequest()->getParams();
  44.          $orm = Mage::getModel('helloorm/ormcontent');
  45.          echo 'begin to read data of id:' . $params['id'];
  46.          $orm->load($params['id']);
  47.          $orm->delete();
  48.          echo 'delete new data success of id:'.$params['id'];
  49.      }
  50.   
  51.      public function loadAllAction() {
  52.          //echo 'Hello orm!';
  53.          $ormData = Mage::getModel('helloorm/ormcontent')->getCollection();
  54.          foreach($ormData as $key=>$value)
  55.          {
  56.              echo '<h3>' . $value->getTitle() . '</h3>';
  57.              echo nl2br($value->getContent());
  58.          }
  59.      }
  60.  }


6.新建app\code\local\Eachgame\Helloorm\Model\Ormcontent.php,添加如下内容

  1.   
  2.  class Eachgame_Helloorm_Model_Ormcontent extends Mage_Core_Model_Abstract
  3.  {
  4.      protected function _construct()
  5.      {
  6.          $this->_init('helloorm/ormcontent');
  7.      }
  8.  }


7.新建app\code\local\Eachgame\Helloorm\Model\Mysql4\Ormcontent.php,添加如下内容

  1.   
  2.  class Eachgame_Helloorm_Model_Mysql4_Ormcontent extends Mage_Core_Model_Mysql4_Abstract
  3.  {
  4.      protected function _construct()
  5.      {
  6.          $this->_init('helloorm/ormcontent', 'orm_id');
  7.      }
  8.  }



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,添加如下内容

  1.   
  2.  class Eachgame_Helloorm_Model_Mysql4_Ormcontent_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
  3.  {
  4.      protected function _construct()
  5.      {
  6.          $this->_init('helloorm/ormcontent');
  7.      }
  8.  }


10.在浏览器输入http://www.shop.com/magento1411/helloorm/index/loadAll,浏览器显示数据表orm_contents中所有的数据。

11.新建app\code\local\Eachgame\Helloorm1\Model\Resource\Mysql4\Setup.php文件

  1.   
  2.  class Eachgame_Helloorm_Model_Resource_Mysql4_Setup extends Mage_Core_Model_Resource_Setup 
  3.  {
  4.  }


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的版本信息。

  1.   
  2.  $installer = $this;
  3.  /* @var $installer Mage_Core_Model_Resource_Setup */
  4.   
  5.  $installer->startSetup();
  6.   
  7.  $installer->run('
  8.   
  9.  DROP TABLE IF EXISTS {$this->getTable('orm_contents')};
  10.  CREATE TABLE {$this->getTable('orm_contents')} (
  11.    `orm_id` int(11) NOT NULL auto_increment,
  12.    `title` text,
  13.    `content` text,
  14.    `date` datetime default NULL,
  15.    `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,
  16.    PRIMARY KEY  (`orm_id`)
  17.  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  18.   
  19.  ');
  20.   
  21.  $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,添加如下内容。

  1.   
  2.  $installer = $this;
  3.  /* @var $installer Mage_Core_Model_Resource_Setup */
  4.   
  5.  $installer->startSetup();
  6.   
  7.  $installer->run('
  8.   
  9.  alter table {$this->getTable('orm_contents')}
  10.  CHANGE title title varchar(255) not null;
  11.   
  12.  ');
  13.   
  14.  $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。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值