Search code examples
magentomagento-1.7magento-1.9magento2

How to create a table in the database in Magento using install script


I am new to Magento.I am trying to create a table in the database using a install script.THE WEB SITE IS HOSTED.I followed few tutorials.They all look same.I followed every step,but the table is not created .Can some one tell me where I have gone wrong? These are the steps I follwed. First I created the folder called Sitepoint inside app/code.Then I created a Articles folder inside Sitepont.This is how it looks

app/code/local/Sitepoint/Articles

Then I created etc folder inside the sitepoint

app/code/local/Sitepoint/Articles/etc

etc floder consist of config.xml file.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 folders for sql and articles_setup in the following way app/code/local/Sitepoint/Articles/sql/articles_setup Inside that it, contains mysql4-install-0.1.0.php file 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.They all provide the same way.Can someone help me?


Solution

  • create Sitepoint_Articles.xml file in app/etc/modules folder and past the following code there. Clear cache and reload the website. Navigate to system->configuration-advanced->advance and make sure your module is listed there. If it appears, check if the table has been created.

    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>