How to create table and install sample data in Magento 2
<?php
namespace Rsgitech\News\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)
{
$newsTableName = $setup->getTable('rsgitech_news');
if($setup->getConnection()->isTableExists($newsTableName) != true) {
$newsTable = $setup->getConnection()
->newTable($newsTableName)
->addColumn(
'news_id',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
null,
['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
'News ID'
)
->addColumn(
'title',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
255,
['nullable' => false, 'default' => ''],
'Title'
)
->addColumn(
'description',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
null,
['nullable' => false, 'default' => ''],
'Description'
)
->addColumn(
'status',
\Magento\Framework\DB\Ddl\Table::TYPE_BOOLEAN,
null,
['nullable' => false, 'unsigned' => true],
'Status'
)
->addColumn(
'created_at',
\Magento\Framework\DB\Ddl\Table::TYPE_DATETIME,
null,
['nullable' => false],
'Created At'
)
->addColumn(
'updated_at',
\Magento\Framework\DB\Ddl\Table::TYPE_DATETIME,
null,
['nullable' => false],
'Updated At'
)
->addIndex(
$setup->getIdxName('rsgitech_news', ['title']),
['title']
)
->setComment("News Table");
$setup->getConnection()->createTable($newsTable);
}
}
}
?>
Install Sample Data
Rsgitech\News\Setup\InstallData.php
You can install sample data by creating InstallData.php
<?php
namespace Rsgitech\News\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
protected $date;
public function __construct(
\Magento\Framework\Stdlib\DateTime\DateTime $date
) {
$this->date = $date;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$dataNewsRows = [
[
'title' => 'News Title 1',
'description' => 'Here is write news description 1',
'status' => 1,
'updated_at' => $this->date->date(),
'created_at' => $this->date->date()
],
[
'title' => 'News Title 2',
'description' => 'Here is write news description 2',
'status' => 1,
'updated_at' => $this->date->date(),
'created_at' => $this->date->date()
]
];
foreach($dataNewsRows as $data) {
$setup->getConnection()->insert($setup->getTable('rsgitech_news'), $data);
}
}
}
?>
Delete tables during extension uninstall
Rsgitech\News\Setup\Uninstall.php
You can delete tables during uninstall extension by command
<?php
namespace Rsgitech\News\Setup;
use Magento\Framework\Setup\UninstallInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
class Uninstall implements UninstallInterface
{
public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->getConnection()->dropTable($setup->getTable('rsgitech_news'));
}
}
?>
Then run below commands
php bin/magento setup:upgrade
php bin/magetno setup:static-content:deploy
php bin/magento cache:flush
0 comments:
Post a Comment
Thanks