2

I want to create table using install schema in magento2 with column collation( utf8_general_ci) .

how to do this?

1
  • Happy to help !! Happy Coding :) Oct 26, 2018 at 9:30

1 Answer 1

4

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();
    }
}
2
  • 2
    Good answer. So we can change charset and collate from setOption.
    – Amit Bera
    Oct 26, 2018 at 9:37
  • Thanks @AmitBera. I take reference from /vendor/magento/framework/DB/Ddl/Table.php file Oct 26, 2018 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.