4

I am new to Magento, and I am trying to create a table in the database using an install script. THE WEB SITE IS HOSTED. I followed few tutorials, and they all look same. I followed every step, but the table is not created. Can some one tell me where I have gone wrong? I created the following file:

app/code/local/Sitepoint/Articles/etc/config.xml

It contains following code:

<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>

Then I created following file:

app/code/local/Sitepoint/Articles/sql/articles_setup/mysql4-install-0.1.0.php

Which has the following code:

<?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();
?>

I followed this tutorial => https://www.sitepoint.com/magento-install-upgrade-data-scripts-explained/ but the tables are not created. I tried few other tutorials, and they all provide the same way. Can someone help me?

1
  • So are you using magento 1 or magento 2? Commented Nov 13, 2017 at 10:26

3 Answers 3

1

You have to specify the version of the module in the config.xml in your etc module folder. See below:

<?xml version="1.0"?>
<config>
    <modules>
        <Sitepoint_Articles>
            <version>0.1.0</version> 
        </Sitepoint_Articles>
    </modules>
    <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>
</config>

And make sure that you registered the modules from

app/etc/modules/Sitepoint_Articles.xml and with a content:

<?xml version="1.0"?>
<config>
    <modules>
        <Sitepoint_Articles>
        <active>true</active>
        <codePool>local</codePool>
        </Sitepoint_Articles>
    </modules>
</config>

Hope that helps.

1

please check your setup_module table and if you find your module then

delete from setup_module where module like '%your_module%';

then run from the command line

php magento\bin setup:upgrade
1
  • He is not using Magento 2, he is using Magento 1 based on the structure of the files.
    – Ner
    Commented Nov 14, 2017 at 0:48
1

Create a new file mysql4-upgrade-0.1.0-0.1.1.php and put same below code in it

<?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();
?>

second thing you need to change version in app/code/local/Sitepoint/Articles/etc/config.xml file.

just change from

<modules>
        <Sitepoint_Articles>
            <version>0.1.0</version> 
        </Sitepoint_Articles>
    </modules>

to.

<modules>
        <Sitepoint_Articles>
            <version>0.1.1</version> 
        </Sitepoint_Articles>
    </modules>

This is the steps for upgrade script.

Hope this will help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.