0

Actually I have created a custom grid in back-end for creating custom table there I am providing some fields like- table name, Assigned to Module Name, Number of Input text boxes. Now I want to generate table using Generate button.

  • You have to create table right? – Chirag Patel Apr 2 at 13:06
  • @ChiragPatel,yes chirag – hitesh balpande Apr 2 at 13:07
  • @hitesh balpande , please change the question title and content. Your target is created table programmatically but your asked that how to create InstallSchema.php using code bother are different – Amit Bera Apr 2 at 13:13
  • @AmitBera,thanks – hitesh balpande Apr 2 at 13:14
  • @hiteshbalpande you cannot create a file. please describe your question in details. what do you want to do after creating Installdata.php file? what is your goal? – Chirag Patel Apr 2 at 13:18
0

You can use following code in your file to create table.

protected $resource;

public function __construct(
    \Magento\Framework\App\ResourceConnection $resource
)
{
    $this->resource = $resource;
}

public function execute()
{
    $this->createTable();
    $this->createTableSchema();
}

public function createTableCore()
{
    $connection = $this->resource->getConnection();
    $tableName = $this->resource->getTableName('your_table_name');

    $sql = "CREATE TABLE `$tableName` (
     `config_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Config Id',
     `scope` varchar(8) NOT NULL DEFAULT 'default' COMMENT 'Config Scope',
     `scope_id` int(11) NOT NULL DEFAULT '0' COMMENT 'Config Scope Id',
     `path` varchar(255) NOT NULL DEFAULT 'general' COMMENT 'Config Path',
     `value` text COMMENT 'Config Value',
     PRIMARY KEY (`config_id`),
     UNIQUE KEY `CORE_CONFIG_DATA_SCOPE_SCOPE_ID_PATH` (`scope`,`scope_id`,`path`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='Your Comment'";

    $connection->query($sql);
}

public function createTableSchema()
{
    $setup = $this->resource;

    $table = $setup->getConnection()->newTable(
            $setup->getTable('your_table_name')
            )->addColumn(
                'id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                null,
                ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true]
            )->addColumn(
                'title',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255
            )->addColumn(
                'created_at',
                \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                null,
                ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT]
            )->addColumn(
                'updated_at',
                \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                null,
                ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT_UPDATE]
            );

        $setup->getConnection()->createTable($table);
}

You may choose your way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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