how to add collation( utf8_general_ci) using install schema in magento2?
-
05-02-2021 - |
Question
I want to create table using install schema in magento2 with column collation( utf8_general_ci) .
how to do this?
Solution
Add this below script in InstallSchema.php file :
<?php
namespace VendorName\ModuleName\Setup;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;
class InstallSchema implements InstallSchemaInterface
{
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
// Get vendor_sampletable table
$tableName = $installer->getTable('vendor_sampletable');
// Check if the table already exists
if ($installer->getConnection()->isTableExists($tableName) != true) {
// Create vendor_sampletable table
$table = $installer->getConnection()
->newTable($tableName)
->addColumn(
'id',
Table::TYPE_INTEGER,
null,
[
'identity' => true,
'unsigned' => true,
'nullable' => false,
'primary' => true
],
'ID'
)
->addColumn(
'title',
Table::TYPE_TEXT,
null,
['nullable' => false, 'default' => ''],
'Title'
)
->addColumn(
'description',
Table::TYPE_TEXT,
null,
['nullable' => false, 'default' => ''],
'Description'
)
->addColumn(
'summary',
Table::TYPE_TEXT,
null,
['nullable' => false, 'default' => ''],
'Summary'
)
->addColumn(
'created_at',
Table::TYPE_DATETIME,
null,
['nullable' => false],
'Created At'
)
->addColumn(
'status',
Table::TYPE_SMALLINT,
null,
['nullable' => false, 'default' => '0'],
'Status'
)
->setComment('Table Comment')
->setOption('type', 'InnoDB')
->setOption('charset', 'utf8')
->setOption('collate', 'utf8_general_ci');
$installer->getConnection()->createTable($table);
}
$installer->endSetup();
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange