How to add new table in existing module using UpgradeSchema in Magento 2
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
class UpgradeSchema implements UpgradeSchemaInterface
public function upgrade(SchemaSetupInterface $setup,ModuleContextInterface $context)
$setup->startSetup();
if (version_compare($context->getVersion(), '2.0.0', '<='))
$installer = $setup;
$installer->startSetup();
$table = $installer->getConnection()
->newTable($installer->getTable('my_booster'))
->addColumn(
'review_id',
Table::TYPE_SMALLINT,
null,
['identity' => true, 'nullable' => false, 'primary' => true],
'Review ID'
)
->addColumn('pros', Table::TYPE_TEXT, 255, ['nullable' => false], 'What I like about this product')
->addColumn('cons', Table::TYPE_TEXT, 255, ['nullable' => false], 'What I dislike about this product')
->addColumn('title', Table::TYPE_TEXT, 255, ['nullable' => false], 'Custom Title')
->addColumn('review_comments', Table::TYPE_TEXT, 255, ['nullable' => false], 'Custom Comments')
->addColumn('send_to', Table::TYPE_TEXT, 255, ['nullable' => false], 'Send reply to Customer')
->addColumn('helpful', Table::TYPE_TEXT, 255, ['nullable' => false], 'Was this review helpful?')
->addColumn('product_quality', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Quality ')
->addColumn('Product Value', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Value')
->addColumn('product_price', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Price')
->addColumn('image', Table::TYPE_TEXT, 255, ['nullable' => false], 'Viemo');
$installer->getConnection()->createTable($table);
$installer->endSetup();
I am using this script.
magento2 database upgradeschema
add a comment |Â
up vote
2
down vote
favorite
class UpgradeSchema implements UpgradeSchemaInterface
public function upgrade(SchemaSetupInterface $setup,ModuleContextInterface $context)
$setup->startSetup();
if (version_compare($context->getVersion(), '2.0.0', '<='))
$installer = $setup;
$installer->startSetup();
$table = $installer->getConnection()
->newTable($installer->getTable('my_booster'))
->addColumn(
'review_id',
Table::TYPE_SMALLINT,
null,
['identity' => true, 'nullable' => false, 'primary' => true],
'Review ID'
)
->addColumn('pros', Table::TYPE_TEXT, 255, ['nullable' => false], 'What I like about this product')
->addColumn('cons', Table::TYPE_TEXT, 255, ['nullable' => false], 'What I dislike about this product')
->addColumn('title', Table::TYPE_TEXT, 255, ['nullable' => false], 'Custom Title')
->addColumn('review_comments', Table::TYPE_TEXT, 255, ['nullable' => false], 'Custom Comments')
->addColumn('send_to', Table::TYPE_TEXT, 255, ['nullable' => false], 'Send reply to Customer')
->addColumn('helpful', Table::TYPE_TEXT, 255, ['nullable' => false], 'Was this review helpful?')
->addColumn('product_quality', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Quality ')
->addColumn('Product Value', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Value')
->addColumn('product_price', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Price')
->addColumn('image', Table::TYPE_TEXT, 255, ['nullable' => false], 'Viemo');
$installer->getConnection()->createTable($table);
$installer->endSetup();
I am using this script.
magento2 database upgradeschema
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
class UpgradeSchema implements UpgradeSchemaInterface
public function upgrade(SchemaSetupInterface $setup,ModuleContextInterface $context)
$setup->startSetup();
if (version_compare($context->getVersion(), '2.0.0', '<='))
$installer = $setup;
$installer->startSetup();
$table = $installer->getConnection()
->newTable($installer->getTable('my_booster'))
->addColumn(
'review_id',
Table::TYPE_SMALLINT,
null,
['identity' => true, 'nullable' => false, 'primary' => true],
'Review ID'
)
->addColumn('pros', Table::TYPE_TEXT, 255, ['nullable' => false], 'What I like about this product')
->addColumn('cons', Table::TYPE_TEXT, 255, ['nullable' => false], 'What I dislike about this product')
->addColumn('title', Table::TYPE_TEXT, 255, ['nullable' => false], 'Custom Title')
->addColumn('review_comments', Table::TYPE_TEXT, 255, ['nullable' => false], 'Custom Comments')
->addColumn('send_to', Table::TYPE_TEXT, 255, ['nullable' => false], 'Send reply to Customer')
->addColumn('helpful', Table::TYPE_TEXT, 255, ['nullable' => false], 'Was this review helpful?')
->addColumn('product_quality', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Quality ')
->addColumn('Product Value', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Value')
->addColumn('product_price', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Price')
->addColumn('image', Table::TYPE_TEXT, 255, ['nullable' => false], 'Viemo');
$installer->getConnection()->createTable($table);
$installer->endSetup();
I am using this script.
magento2 database upgradeschema
class UpgradeSchema implements UpgradeSchemaInterface
public function upgrade(SchemaSetupInterface $setup,ModuleContextInterface $context)
$setup->startSetup();
if (version_compare($context->getVersion(), '2.0.0', '<='))
$installer = $setup;
$installer->startSetup();
$table = $installer->getConnection()
->newTable($installer->getTable('my_booster'))
->addColumn(
'review_id',
Table::TYPE_SMALLINT,
null,
['identity' => true, 'nullable' => false, 'primary' => true],
'Review ID'
)
->addColumn('pros', Table::TYPE_TEXT, 255, ['nullable' => false], 'What I like about this product')
->addColumn('cons', Table::TYPE_TEXT, 255, ['nullable' => false], 'What I dislike about this product')
->addColumn('title', Table::TYPE_TEXT, 255, ['nullable' => false], 'Custom Title')
->addColumn('review_comments', Table::TYPE_TEXT, 255, ['nullable' => false], 'Custom Comments')
->addColumn('send_to', Table::TYPE_TEXT, 255, ['nullable' => false], 'Send reply to Customer')
->addColumn('helpful', Table::TYPE_TEXT, 255, ['nullable' => false], 'Was this review helpful?')
->addColumn('product_quality', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Quality ')
->addColumn('Product Value', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Value')
->addColumn('product_price', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Price')
->addColumn('image', Table::TYPE_TEXT, 255, ['nullable' => false], 'Viemo');
$installer->getConnection()->createTable($table);
$installer->endSetup();
I am using this script.
magento2 database upgradeschema
asked Aug 13 at 22:34
Ali Ejaz
1209
1209
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
Try this
class UpgradeSchema implements UpgradeSchemaInterface
{
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
$installer = $setup;
$setup->startSetup();
$version = $context->getVersion();
$connection = $setup->getConnection();
if (version_compare($version, '2.0.0') < 0)
$connection->addColumn(
$setup->getTable('my_booster'),
'author_id',
[
'type' => MagentoFrameworkDBDdlTable::TYPE_INTEGER,
'nullable' => true,
'comment' => 'Author ID',
]
);
kindly mention the issue as well.
â Ali Ejaz
Aug 13 at 22:46
here is the issue if (version_compare($context->getVersion(), '2.0.0', '<=')) , withi which you are comparing
â sheraz khan
Aug 13 at 22:48
try this if (version_compare($version, '2.0.0') < 0)
â sheraz khan
Aug 13 at 22:49
your answer is not working for me.
â Ali Ejaz
Aug 13 at 22:53
add a comment |Â
up vote
1
down vote
accepted
i have got the correct working answer.
<?php
namespace VendorModuleSetup;
use MagentoFrameworkSetupUpgradeSchemaInterface;
use MagentoFrameworkSetupSchemaSetupInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkDBDdlTable;
class UpgradeSchema implements UpgradeSchemaInterface
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
$setup->startSetup();
if (version_compare($context->getVersion(), '2.0.2', '<'))
$setup->getConnection()->addColumn(
$setup->getTable("enter table name here"),
'enter new column name here',
[
"type" => Table::TYPE_TEXT,
"nullable" => true,
"comment" => "xample"
]
);
$setup->getConnection()->addColumn(
$setup->getTable("enter table name here"),
'enter new second column name here',
[
"type" => Table::TYPE_TEXT,
"nullable" => true,
"comment" => "Xample 2"
]
);
$setup->getConnection()->addColumn(
$setup->getTable("enter table name here"),
'enter new third column name here',
[
"type" => Table::TYPE_TEXT,
"nullable" => true,
"comment" => "Xample 3"
]
);
$setup->endSetup();
also you need to change etc/module.xml file. where setup_version="2.0.1" schema_version="2.0.1"
.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Try this
class UpgradeSchema implements UpgradeSchemaInterface
{
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
$installer = $setup;
$setup->startSetup();
$version = $context->getVersion();
$connection = $setup->getConnection();
if (version_compare($version, '2.0.0') < 0)
$connection->addColumn(
$setup->getTable('my_booster'),
'author_id',
[
'type' => MagentoFrameworkDBDdlTable::TYPE_INTEGER,
'nullable' => true,
'comment' => 'Author ID',
]
);
kindly mention the issue as well.
â Ali Ejaz
Aug 13 at 22:46
here is the issue if (version_compare($context->getVersion(), '2.0.0', '<=')) , withi which you are comparing
â sheraz khan
Aug 13 at 22:48
try this if (version_compare($version, '2.0.0') < 0)
â sheraz khan
Aug 13 at 22:49
your answer is not working for me.
â Ali Ejaz
Aug 13 at 22:53
add a comment |Â
up vote
2
down vote
Try this
class UpgradeSchema implements UpgradeSchemaInterface
{
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
$installer = $setup;
$setup->startSetup();
$version = $context->getVersion();
$connection = $setup->getConnection();
if (version_compare($version, '2.0.0') < 0)
$connection->addColumn(
$setup->getTable('my_booster'),
'author_id',
[
'type' => MagentoFrameworkDBDdlTable::TYPE_INTEGER,
'nullable' => true,
'comment' => 'Author ID',
]
);
kindly mention the issue as well.
â Ali Ejaz
Aug 13 at 22:46
here is the issue if (version_compare($context->getVersion(), '2.0.0', '<=')) , withi which you are comparing
â sheraz khan
Aug 13 at 22:48
try this if (version_compare($version, '2.0.0') < 0)
â sheraz khan
Aug 13 at 22:49
your answer is not working for me.
â Ali Ejaz
Aug 13 at 22:53
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Try this
class UpgradeSchema implements UpgradeSchemaInterface
{
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
$installer = $setup;
$setup->startSetup();
$version = $context->getVersion();
$connection = $setup->getConnection();
if (version_compare($version, '2.0.0') < 0)
$connection->addColumn(
$setup->getTable('my_booster'),
'author_id',
[
'type' => MagentoFrameworkDBDdlTable::TYPE_INTEGER,
'nullable' => true,
'comment' => 'Author ID',
]
);
Try this
class UpgradeSchema implements UpgradeSchemaInterface
{
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
$installer = $setup;
$setup->startSetup();
$version = $context->getVersion();
$connection = $setup->getConnection();
if (version_compare($version, '2.0.0') < 0)
$connection->addColumn(
$setup->getTable('my_booster'),
'author_id',
[
'type' => MagentoFrameworkDBDdlTable::TYPE_INTEGER,
'nullable' => true,
'comment' => 'Author ID',
]
);
edited Aug 13 at 22:47
answered Aug 13 at 22:43
sheraz khan
918
918
kindly mention the issue as well.
â Ali Ejaz
Aug 13 at 22:46
here is the issue if (version_compare($context->getVersion(), '2.0.0', '<=')) , withi which you are comparing
â sheraz khan
Aug 13 at 22:48
try this if (version_compare($version, '2.0.0') < 0)
â sheraz khan
Aug 13 at 22:49
your answer is not working for me.
â Ali Ejaz
Aug 13 at 22:53
add a comment |Â
kindly mention the issue as well.
â Ali Ejaz
Aug 13 at 22:46
here is the issue if (version_compare($context->getVersion(), '2.0.0', '<=')) , withi which you are comparing
â sheraz khan
Aug 13 at 22:48
try this if (version_compare($version, '2.0.0') < 0)
â sheraz khan
Aug 13 at 22:49
your answer is not working for me.
â Ali Ejaz
Aug 13 at 22:53
kindly mention the issue as well.
â Ali Ejaz
Aug 13 at 22:46
kindly mention the issue as well.
â Ali Ejaz
Aug 13 at 22:46
here is the issue if (version_compare($context->getVersion(), '2.0.0', '<=')) , withi which you are comparing
â sheraz khan
Aug 13 at 22:48
here is the issue if (version_compare($context->getVersion(), '2.0.0', '<=')) , withi which you are comparing
â sheraz khan
Aug 13 at 22:48
try this if (version_compare($version, '2.0.0') < 0)
â sheraz khan
Aug 13 at 22:49
try this if (version_compare($version, '2.0.0') < 0)
â sheraz khan
Aug 13 at 22:49
your answer is not working for me.
â Ali Ejaz
Aug 13 at 22:53
your answer is not working for me.
â Ali Ejaz
Aug 13 at 22:53
add a comment |Â
up vote
1
down vote
accepted
i have got the correct working answer.
<?php
namespace VendorModuleSetup;
use MagentoFrameworkSetupUpgradeSchemaInterface;
use MagentoFrameworkSetupSchemaSetupInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkDBDdlTable;
class UpgradeSchema implements UpgradeSchemaInterface
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
$setup->startSetup();
if (version_compare($context->getVersion(), '2.0.2', '<'))
$setup->getConnection()->addColumn(
$setup->getTable("enter table name here"),
'enter new column name here',
[
"type" => Table::TYPE_TEXT,
"nullable" => true,
"comment" => "xample"
]
);
$setup->getConnection()->addColumn(
$setup->getTable("enter table name here"),
'enter new second column name here',
[
"type" => Table::TYPE_TEXT,
"nullable" => true,
"comment" => "Xample 2"
]
);
$setup->getConnection()->addColumn(
$setup->getTable("enter table name here"),
'enter new third column name here',
[
"type" => Table::TYPE_TEXT,
"nullable" => true,
"comment" => "Xample 3"
]
);
$setup->endSetup();
also you need to change etc/module.xml file. where setup_version="2.0.1" schema_version="2.0.1"
.
add a comment |Â
up vote
1
down vote
accepted
i have got the correct working answer.
<?php
namespace VendorModuleSetup;
use MagentoFrameworkSetupUpgradeSchemaInterface;
use MagentoFrameworkSetupSchemaSetupInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkDBDdlTable;
class UpgradeSchema implements UpgradeSchemaInterface
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
$setup->startSetup();
if (version_compare($context->getVersion(), '2.0.2', '<'))
$setup->getConnection()->addColumn(
$setup->getTable("enter table name here"),
'enter new column name here',
[
"type" => Table::TYPE_TEXT,
"nullable" => true,
"comment" => "xample"
]
);
$setup->getConnection()->addColumn(
$setup->getTable("enter table name here"),
'enter new second column name here',
[
"type" => Table::TYPE_TEXT,
"nullable" => true,
"comment" => "Xample 2"
]
);
$setup->getConnection()->addColumn(
$setup->getTable("enter table name here"),
'enter new third column name here',
[
"type" => Table::TYPE_TEXT,
"nullable" => true,
"comment" => "Xample 3"
]
);
$setup->endSetup();
also you need to change etc/module.xml file. where setup_version="2.0.1" schema_version="2.0.1"
.
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
i have got the correct working answer.
<?php
namespace VendorModuleSetup;
use MagentoFrameworkSetupUpgradeSchemaInterface;
use MagentoFrameworkSetupSchemaSetupInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkDBDdlTable;
class UpgradeSchema implements UpgradeSchemaInterface
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
$setup->startSetup();
if (version_compare($context->getVersion(), '2.0.2', '<'))
$setup->getConnection()->addColumn(
$setup->getTable("enter table name here"),
'enter new column name here',
[
"type" => Table::TYPE_TEXT,
"nullable" => true,
"comment" => "xample"
]
);
$setup->getConnection()->addColumn(
$setup->getTable("enter table name here"),
'enter new second column name here',
[
"type" => Table::TYPE_TEXT,
"nullable" => true,
"comment" => "Xample 2"
]
);
$setup->getConnection()->addColumn(
$setup->getTable("enter table name here"),
'enter new third column name here',
[
"type" => Table::TYPE_TEXT,
"nullable" => true,
"comment" => "Xample 3"
]
);
$setup->endSetup();
also you need to change etc/module.xml file. where setup_version="2.0.1" schema_version="2.0.1"
.
i have got the correct working answer.
<?php
namespace VendorModuleSetup;
use MagentoFrameworkSetupUpgradeSchemaInterface;
use MagentoFrameworkSetupSchemaSetupInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkDBDdlTable;
class UpgradeSchema implements UpgradeSchemaInterface
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
$setup->startSetup();
if (version_compare($context->getVersion(), '2.0.2', '<'))
$setup->getConnection()->addColumn(
$setup->getTable("enter table name here"),
'enter new column name here',
[
"type" => Table::TYPE_TEXT,
"nullable" => true,
"comment" => "xample"
]
);
$setup->getConnection()->addColumn(
$setup->getTable("enter table name here"),
'enter new second column name here',
[
"type" => Table::TYPE_TEXT,
"nullable" => true,
"comment" => "Xample 2"
]
);
$setup->getConnection()->addColumn(
$setup->getTable("enter table name here"),
'enter new third column name here',
[
"type" => Table::TYPE_TEXT,
"nullable" => true,
"comment" => "Xample 3"
]
);
$setup->endSetup();
also you need to change etc/module.xml file. where setup_version="2.0.1" schema_version="2.0.1"
.
answered Aug 16 at 23:28
Ali Ejaz
1209
1209
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f238251%2fhow-to-add-new-table-in-existing-module-using-upgradeschema-in-magento-2%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password