3

I want to create a table with one default row.

i.e. I have two attributes. How to create table with (1,1) entry by default using Install Schema?

3

In addition to Dave's answer which use InstallData.php, we could use InstallSchema.php too, which is more simple and codes like below, notice the 'default' => 1 in addColumn method.

<?php

namespace Vendor\Module\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();

        try {
            $table = $installer->getConnection()->newTable(
                $installer->getTable('table_name')
            )
                ->addColumn(
                    'number',
                    Table::TYPE_INTEGER,
                    null,
                    ['nullable' => false, 'unsigned' => true, 'default' => 1]
                )
                ->addColumn(
                    'number_2',
                    Table::TYPE_INTEGER,
                    null,
                    ['default' => 1]
                )
            ;

            $installer->getConnection()->createTable($table);
        } catch (\Zend_Db_Exception $e) {
        }

        $installer->endSetup();
    }
}
2

If you want add default data in your table like first row , You need to create InstallData.php file in your setup folder

Try below code :

<?php
namespace Vendor\Magento\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

class InstallData implements InstallDataInterface {


    /**
     * 
     * Install Data in custom table
     *   
     */
    public function install( ModuleDataSetupInterface $setup, ModuleContextInterface $context ) {
        $setup->startSetup();

        $tableName = $setup->getTable('your_table_name');

        if ($setup->getConnection()->isTableExists($tableName) == true) {
        $tabledata = [
        [
            'Id' => 1,
            'Code' => 1,

        ]
        ];
        foreach ($tabledata as $data) {
                $setup->getConnection()->insert($tableName, $data);
            } 
        }
         $setup->endSetup();
    }
} 

Note : Basically installschema is used for creating the table structure while installdata is used for add data in that created table.

Refer to know difference between both - Difference between InstallSchema and InstallData

At the end don't forgot to run php bin/magento setup:upgrade command.

0

Write a InstallData in your setup folder

 use Magento\Framework\Setup\InstallDataInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\ModuleDataSetupInterface;

    class InstallData implements InstallDataInterface
    {

        public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
        {
            $setup->startSetup();

            $attribute_name= [
                [
                    'attributes_id' => 1,
                    'code' => '1',               
                ],

            ];

            $setup->getConnection()->insertMultiple($setup->getTable('your_tablename'), $attribute_name);

            $setup->endSetup();
        }
    }

run set:up it will insert default table

0

Write Like this

$columns = [
                'cartcheck' => [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                    'nullable' => true,
                    ['default' => '0'],
                    'comment' => 'Cart Checkbox',
                ],
            ];

I Hope You Get The Correct Answer

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.