mysql - 在magento模块中创建表

标签 mysql sql magento-1.7 magento-1.9 magento-1.8

我是 magento 的新手。如何在数据库中创建表?我尝试了一些代码,但表没有创建。但在 core_resource 表中我已经设置了我的表。我的 php 文件的版本与我的配置文件的版本匹配。请帮助我,我两天来寻找这个问题的答案,但无处可寻。

最佳答案

创建表格的方法有很多种。

  1. 可以直接phpmyadmin创建表
  2. 编写自己的sql并通过php文件运行

这两种方式也适用于 magento。

但是如果您尝试通过自己的自定义模块创建自己的表,那么您必须使用xml和magento models

我为您提供了一个模块示例。

XML

 <models>
        <magenotification>
            <class>Magestore_Magenotification_Model</class>
            <resourceModel>magenotification_mysql4</resourceModel>
        </magenotification>
        <magenotification_mysql4>
            <class>Magestore_Magenotification_Model_Mysql4</class>
            <entities>
                <magenotification>
                    <table>magenotification</table>
                </magenotification>
                <feedback>
                    <table>magenotification_extension_feedback</table>
                </feedback>         
                <feedbackmessage>
                    <table>magenotification_extension_feedbackmessage</table>
                </feedbackmessage>                          
                <logger>
                    <table>magenotification_log</table>
                </logger>
                <license>
                    <table>magenotification_license</table>
                </license>
            </entities>
        </magenotification_mysql4>
    </models>

SQL

<?php

$installer = $this;

$installer->startSetup();

$installer->run("


DROP TABLE IF EXISTS {$this->getTable('magenotification_extension_feedbackmessage')};
DROP TABLE IF EXISTS {$this->getTable('magenotification_extension_feedback')};
DROP TABLE IF EXISTS {$this->getTable('magenotification_log')};
DROP TABLE IF EXISTS {$this->getTable('magenotification')};

CREATE TABLE {$this->getTable('magenotification')} (
  `magenotification_id` int(11) unsigned NOT NULL auto_increment,
  `notification_id` int(10) unsigned NOT NULL,
  `url` varchar(255) NOT NULL default '',
  `added_date` datetime NOT NULL,
  UNIQUE (`notification_id`, `url`),
  PRIMARY KEY (`magenotification_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE {$this->getTable('magenotification_log')} (
  `log_id` int(11) unsigned NOT NULL auto_increment,
  `extension_code` varchar(100) NOT NULL default '',
  `license_type` varchar(50) NOT NULL default '',
  `license_key` text NOT NULL default '',
  `check_date` date NOT NULL,
  `sum_code` varchar(255),
  `response_code` smallint(5),
  `expired_time` varchar(255),
  `is_valid` tinyint(1),
  PRIMARY KEY (`log_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE {$this->getTable('magenotification_extension_feedback')} (
  `feedback_id` int(11) unsigned NOT NULL auto_increment,
  `code` varchar(255) NOT NULL default '',
  `extension` varchar(255) NOT NULL default '',
  `extension_version` varchar(50) NOT NULL default '',
  `coupon_code` varchar(255) NOT NULL default '',
  `coupon_value` varchar(50) NOT NULL default '',
  `expired_counpon` datetime NOT NULL,
  `content` text NOT NULL default '',
  `file` text NOT NULL default '',
  `comment` text NOT NULL default '',
  `latest_message` text NOT NULL default '',  
  `latest_response` text NOT NULL default '',
  `latest_response_time` datetime,
  `status` tinyint(1) NOT NULL DEFAULT '3',
  `is_sent` tinyint(1) NOT NULL DEFAULT '2',
  `created` datetime NOT NULL,
  `updated` datetime NOT NULL,
  PRIMARY KEY (`feedback_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE {$this->getTable('magenotification_extension_feedbackmessage')} (
  `feedbackmessage_id` int(11) unsigned NOT NULL auto_increment,
  `feedback_id` int(11) unsigned NOT NULL,
  `feedback_code` varchar(255) NOT NULL default '',
  `user` varchar(255) NOT NULL default '',
  `is_customer` tinyint(1) default '2',
  `message` text NOT NULL default '',
  `file` text NOT NULL default '',
  `posted_time` datetime NULL,
  `is_sent` tinyint(1) default '2',
  PRIMARY KEY (`feedbackmessage_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    ");

$installer->endSetup(); 

你必须学习如何create custom module在 magento 中了解更多信息。

custom module with database

关于mysql - 在magento模块中创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34292903/

相关文章:

sql - 执行附加连接时,Count (*) 是否会扭曲所需的结果?

mysql - 更改 Magento 数据库

从购物车 magento 隐藏小计

mysql分钟到小时和分钟

php - 如何设置 SQL 表以高效、轻松地存储单个事件的多个日期和时间?

MySQL返回大小写值

mysql - 如何在 mysql 中将姓氏和名字分成两个新列?

sql - 在sql server 2008 R2上将多个链接服务器从一个服务器迁移到另一个

mysql - 由于学生和科目大小不固定,如何在将学生分数插入 tran 表时减少值的冗余?

php - 如何在 magento 中获取页面标题?