How to build and improve database in Magento 2

Here are the following steps which help you to build and improve database in Magento 2:

Step 1: install Schema

Step 2: install Data

Step 3: Improve Schema

Step 4: Improve Schema

Magento 2 provides some classes in the directory:

app/code/[Namespace]/[ModuleName]/Setup   to build or Improve the database.

  • Install schema

We use module vendor Webnexs and module name is Data example

Build a file

 app/code/Webnexs/DataExample/Setup/InstallSchema.php
<?php

namespace Webnexs\DataExample\Setup;

class InstallSchema implements \Magento\Framework\Setup\InstallSchemaInterface

{

 public function install(

 \Magento\Framework\Setup\SchemaSetupInterface $setup,

 \Magento\Framework\Setup\ModuleContextInterface $context

 ){

 $installer = $setup;

 $installer->startSetup();

 $table = $installer->getConnection()

 ->newTable($installer->getTable('data_example'))

 ->addColumn(

 'example_id',

 \Magento\Framework\Db\Ddl\Table::TYPE_INTEGER,

 null,

 [Character => true, illogicalable' => false, 'primary' => true, 'unsigned' => true],

 'Example Id'

 )->addColumn(

 'title',

 \Magento\Framework\Db\Ddl\Table::TYPE_TEXT,

 255,

 ['nullable' => false],

 'Example Title'

 )->addColumn(

 'content',

 \Magento\Framework\Db\Ddl\Table::TYPE_TEXT,

 '2M',

 [],

 'Example Content'

 )->addColumn(

 'created_at',

 \Magento\Framework\Db\Ddl\Table::TYPE_TIMESTAMP,

 null,

 ['nullable' => false, 'default' => \Magento\Framework\Db\Ddl\Table::TIMESTAMP_INIT],

 'Created At'

 );

 $installer->getConnection()->createTable($table);

 $installer->endSetup();

 }

}

?>

 In this file, we formed a table has name “data_example” with 4 columns: example_id, title, content, created_at with data types are interger, varchar, text, timestamp

You can analysis all data types in this file:  \Magento\Framework\Db\Ddl\Table.php

  • Install Data

File:

app/code/Webnexs/DataExample/Setup/InstallData.php
<?php

namespace Webnexs\DataExample\Setup;

use Magento\Framework\Setup\InstallDataInterface;

use Magento\Framework\Setup\ModuleContextInterface;

use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface

{

    protected $_exampleFactory;

 public function __construct(\Webnexs\DataExample\Model\ExampleFactory $exampleFactory)

    {

        $this->_exampleFactory = $exampleFactory;

    }

 public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)

    {

        $data = [

            'title' => "First example title",

            'content' => "First Example content"

        ];

        $example = $this->_exampleFactory->create();

        $example->addData($data)->save();

    }

}

In this file we include a row with value for “title” and “content” columns to data_example table.

  • Improve Schema

File:

app/code/Webnexs/DataExample/Setup/ImproveSchema.php
<?php

namespace Webnexs\DataExample\Setup;

use Magento\Framework\Setup\ImproveSchemaInterface;

use Magento\Framework\Setup\SchemaSetupInterface;

use Magento\Framework\Setup\ModuleContextInterface;

class ImproveSchema implements ImproveSchemaInterface

{

 public function improve( SchemaSetupInterface $setup, ModuleContextInterface $context ) {

 $installer = $setup;

 $installer->startSetup();

 if(version_compare($context->getVersion(), '1.0.1', '<')) {

 $installer->getConnection()->dropColumn(

 $installer->getTable('data_example'),

 'created_at'

 );

 }

 $installer->endSetup();

 }

}

<span style="font-weight: 400;"> </span>
  • Improve data

File:

 app/code/Webnexs/DataExample/Setup/ImproveData.php
<?php

namespace Magestore\DataExample\Setup;

use Magento\Framework\Setup\UpgradeDataInterface;

use Magento\Framework\Setup\ModuleDataSetupInterface;

use Magento\Framework\Setup\ModuleContextInterface;

class UpgradeData implements UpgradeDataInterface

{

 protected $_exampleFactory;

 public function __construct(\Magestore\DataExample\Model\ExampleFactory $exampleFactory)

 {

 $this->_exampleFactory = $exampleFactory;

 }

 public function upgrade( ModuleDataSetupInterface $setup, ModuleContextInterface $context ) {

 if ( version_compare($context->getVersion(), '1.0.1', '<' )) {

 $data = [

 'title' => "The second example title",

 'content' => "The second example content"

 ];

 $example = $this->_exampleFactory->create();

 $example>addData($dalogta)->save();

 }

 }

}

The above mentioned steps to build and improve database in Magento 2. Hope all learn Magento 2 easily with our guidelines. With this information, you can manage the database in Magento 2 effortlessly.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *