How to Add a New Table to a Database in Magento Module

How to Add a New Table to a Database in Magento Module

This tutorial applies to Magento 2.2.x only. For Magento 2.3.x, see Declarative

1. QuickRead

Magento 2 has a special mechanism that allows you to create database tables, change existing ones, and even add some data to them (like setup data, which has to be added when a module is installed). This mechanism enables these modifications to be transferable between various installations.
The main concept is that instead of performing manual SQL operations that you have to do again and again when reinstalling the system, developers are creating an install (or upgrade) script that contains the data. The script will run any time a module is installed.
In the previous post, we learned how to build a single custom module. Following the previous tutorial, we will learn how to build a database table in Magento 2.
This is a very simple tutorial, but it’s very relevant. You’re going to apply it a lot. Let’s get started!

Related Articles

Contact Us

2. File Structure of Module

We’ve updated our File Module structure as follows:

3. Create file module.xml and registration.php

For the job of creating these 2 files, I have detailed instructions and explanations in the previous article. You can read it here.

4. Create file InstallSchema.php

You create this file according to the path: Oscprofessionals/Mymodule/Setup/InstallSchema.php.
We will create a table name magetop_blog with fields: id, title, description, created_at, status.

5. Create file InstallData.php

Next, we will insert data for the oscp_blog table.
You create this file according to the path:Magetop/Helloworld/Setup/InstallData.php.

6. Increase the version in module.xml

If you have just created this module and you have not declared it on the database yet, you do not need to increase the version in module.xml
If you have already declared this module on the database, please increase the version in module.xml

7. Run the command

You need to run 2 commands:
Please check on the database and You will see your accomplishment.

8. Upgrade table in the database

Next, I will guide you to add fields to the created table and create a new table in UpgradeSchema.php.
You create this file according to the path: Magetop/Helloworld/Setup/InstallData.php.
In the following code, I will implement two field images, category_id, and create a new table magetop_blog_categories.
We will insert data for the oscp_blog_categories table.
Do not forget to increase the version in module.xml!.
Finally, run the two commands above and enjoy the result.
Please check on the database and You will see your accomplishment.
I hope through this series you can create your own complete module. Good luck!

Related Articles

Contact Us

Mar 10th, 2021|
This website or its third party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the privacy policy. By tapping on "I accept" you agree to the use of cookies.