【问题标题】:How to create a table in the database in Magento using install script如何使用安装脚本在 Magento 的数据库中创建表
【发布时间】:2018-04-25 08:38:17
【问题描述】:

我是 Magento 的新手。我正在尝试使用安装脚本在数据库中创建一个表。该网站是托管的。我遵循了一些教程。它们看起来都一样。我遵循了每一步,但表不是创建。有人能告诉我哪里出错了吗? 这些是我遵循的步骤。 首先,我在 app/code 中创建了名为 Sitepoint 的文件夹。然后我在 Sitepont 中创建了一个 Articles 文件夹。这就是它的外观

app/code/local/Sitepoint/文章

然后我在站点点内创建了 etc 文件夹

app/code/local/Sitepoint/Articles/等

etc floder由config.xml文件组成。它包含以下代码。

<global>
    <models>
        <articles>
            <class>Sitepoint_Articles_Model</class> <!-- Model class files -->     
            <resourceModel>articles_mysql4</resourceModel> <!--Resource model -->
        </articles>
        <articles_mysql4>
            <class>Sitepoint_Articles_Model_Mysql4</class>
            <entities>
                <articles>
                    <table>articles</table>  <!-- Db table name  -->
                </articles>
            </entities>
        </articles_mysql4>
    </models>
    <resources>  
        <articles_setup>
            <setup>
                <module>Sitepoint_Articles</module>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </articles_setup>
        <articles_write>
            <connection>
                <use>core_write</use>
            </connection>
        </articles_write>
        <articles_read>
            <connection>
                <use>core_read</use>
            </connection>
        </articles_read>
    </resources>
</global>

然后我通过以下方式为sql和articles_setup创建了文件夹 app/code/local/Sitepoint/Articles/sql/articles_setup 在其中,包含 mysql4-install-0.1.0.php 文件,该文件具有以下代码。

<?php
$installer = $this;
$installer->startSetup();
$installer->run("-- DROP TABLE IF EXISTS {$this->getTable('articles')};
CREATE TABLE {$this->getTable('articles')} (
      `articles_id` int(11) unsigned NOT NULL auto_increment,
      `title` varchar(255) NOT NULL default '',
      `short_desc` text NOT NULL default '',
      `long_desc` text NOT NULL default '',
      `status` tinyint(2) NOT NULL default '0',
      `created_time` datetime NULL,
      `update_time` datetime NULL,
      PRIMARY KEY (`articles_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    ");
    $installer->endSetup();
?>

我按照本教程进行操作 => https://www.sitepoint.com/magento-install-upgrade-data-scripts-explained/ 但是没有创建表格。我尝试了一些其他教程。它们都提供相同的方式。有人可以帮助我吗?

【问题讨论】:

  • 你在app/etc/modules 中添加了Sitepoint_Articles.xml 吗?
  • 我已经添加了 Sitepoint_Articles.xml,但是有一个拼写错误。我拼写的是 sitepoint 而不是 SItepoint。谢谢。它现在可以工作了

标签: magento magento-1.7 magento-1.9 magento2


【解决方案1】:

在 app/etc/modules 文件夹中创建 Sitepoint_Articles.xml 文件,并在其中粘贴以下代码。清除缓存并重新加载网站。导航到 system->configuration-advanced->advance 并确保您的模块在此处列出。如果出现,请检查表是否已创建。

app/etc/modules/Sitepoint_Articles.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Sitepoint_Articles>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <!-- add any depending modules here -->
            </depends>
        </Sitepoint_Articles>
    </modules>
</config>

【讨论】:

  • 谢谢老兄。干杯
  • 很高兴你解决了这个问题。没有这个,你的模块对 magento 是不可见的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-25
  • 1970-01-01
  • 2011-08-11
  • 1970-01-01
相关资源
最近更新 更多