Magento 1. Creating a module. Part 4. Adding a table to the database for our module

June 18, 2017 25 Yehor Rykhnov

In this article we will create a table for our module.

Create a table in the database

In Magento it is very convenient to manage the database tables. Here we use next approach:
A file is created in which the table management functionality (creation, editing, etc.) is implemented,
the file name contains the version number (for example: install-1.0.0.php), after updating the page, Magento will perform the functionality described in the file and record it in the core_resource table.

If we create a table, we create a file that starts with install (for example: install-1.0.0.php), to change the table - the file name must begin with update (For example:update-1.0.0.php).

Create folders:

app/code/local/My/Articles/sql
app/code/local/My/Articles/sql/marticles_setup

Create a file:

app/code/local/My/Articles/sql/marticles_setup/install-1.0.0.php

With code:

<?php
die('Setup');
$installer = $this;
$installer->startSetup();
$installer->run("CREATE TABLE my_articles (
      `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
      `title` VARCHAR(255) NOT NULL,
      `header_h1` VARCHAR(255) NOT NULL,
      `meta_tag_keywords` VARCHAR(255) NOT NULL,
      `meta_tag_description` VARCHAR(255) NOT NULL,
      `image` VARCHAR(255) NOT NULL,
      `preview` TEXT NOT NULL,
      `content` TEXT NOT NULL,
      `created` DATETIME,
      PRIMARY KEY (`id`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
$installer->endSetup();

In this file we add parameters (see comments)

Code for adding tables to the database:

<?xml version="1.0" ?>
<config>
    <modules>
        <My_Articles>
            <version>1.0.0</version>
        </My_Articles>
    </modules>
    <!-- Code for controller -->
    <frontend>        
        <routers>
            <marticles>
                <use>standard</use>
                <args>
                    <module>My_Articles</module>
                    <frontName>articles</frontName>
                </args>
            </marticles>
        </routers>
    </frontend>
    <global>
        <!-- Code for adding tables to the database -->
        <myarticles_recource>
            <class>My_Articles_Model_Resource</class>
            <entities>
                <table_myarticles>
                    <table>my_articles</table>
                </table_myarticles>
            </entities>
        </myarticles_recource>
    </global>
</config>

We update the page in the browser. Must see the text: Setup.

Remove the row:

die('Setup');

and again we update the page in the browser.

After that, you should see the my_articles table in the database, and the code: myarticles_setup will appear in the core_resource table.

If you need to reinstall the table - you can remove it from the database and delete the entry in the core_resource table.

After updating any page, the changes will go to the database, and to the core_resource table the record with the part of the block name (in this case myarticles). And the following updates will not run the files of reinstall the table. If we want to add more tables or change something, we create a file with another unique version.