How to upgrade database table in magento 2?

When You need to add a new field in the existing table, you need to create UpgradeSchema.php file in Setup folder of your module.

We add new address field in our existing table by below way, We have just create a database table in the previous post, Create Database table in Magento 2

<?php

namespace Rbj\Training\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;

class UpgradeSchema implements UpgradeSchemaInterface
{

    /**
     * {@inheritdoc}
     */
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        if (version_compare($context->getVersion(), '1.0.1') < 0) {
            $connection = $setup->getConnection();
            $connection->addColumn(
                $setup->getTable('create_form'),
                'address',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'length' => 255,
                    'nullable' => true,
                    'default' => '',
                    'comment' => 'Address field'
                ]
            );
        }
    }
}

Run php bin/magento setup:upgrade

Now you can check your new address field is display in your create_form table.

How to create database table with Install Schema in Magento 2?

In Magento 2, You need to create an InstallSchema.php file in the Setup folder under your module. In my case, the Module name is Rbj_Training,

Below code snippets are created database table name of create_form.

<?php
namespace Rbj\Training\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class InstallSchema implements InstallSchemaInterface
{
    public function install(
        SchemaSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        //Your install script

        $createform = $setup->getConnection()->newTable($setup->getTable('create_form'));

        $createform->addColumn(
            'entity_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            null,
            ['identity' => true,'nullable' => false,'primary' => true,'unsigned' => true,],
            'Entity ID'
        );

        $createform->addColumn(
            'name',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            50,
            [],
            'name'
        );

        $createform->addColumn(
            'phone_no',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            20,
            [],
            'phone_no'
        );

        $createform->addColumn(
            'image',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            [],
            'Image'
        );

        $setup->getConnection()->createTable($createform);
    }
}

Above script create create_form table with 4 fields, entity_id, name, phone_no, image

entity_id is an auto-increment field, a name is for name display, phone_no is used for phone and image is for image name store.

How to create a simple Hello World module in Magento 2?

Creating a Simple Hello World Module using Magento 2 is the Entry point for Learning Magento module structure.

A simple Module to display, Hello World in the browser is used for Beginner of Magento Developer who wants to explore Magento 2 in depth. Continue reading “How to create a simple Hello World module in Magento 2?”