Dhananjay Kulkarni
Dhananjay Kulkarni

Reputation: 1

How to save data in database magento2?

I have integrated theme in magento2, and now I need to change In the registration form of theme.I need to add some field and save in database. But I am new in magento2 and I didnt get the how to find controller for it and whats the flow?

So can any one have ideas about this?regarding how to start on customize in the theme?

Upvotes: 0

Views: 1245

Answers (1)

Sam Cheung
Sam Cheung

Reputation: 31

For add field, you can add field in Setup/UpgradeSchema.php in your custom module:

    namespace Name\moduleName\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;

class UpgradeSchema implements UpgradeSchemaInterface {


    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context) {
        $installer = $setup;
        $installer->startSetup();


        $installer->getConnection()->addColumn(
                $installer->getTable('tablename'), 'field name', [
            'type' => \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
            'nullable' => true,
            'length' => 255,
            'comment' => 'xxxxx'
                ]
        );
   $installer->endSetup();
    }

}

but you should change setup_version in module.xml and setup:upgrade

Or just add in database direcly:

ALTER TABLE table_name
ADD column_name datatype;

Upvotes: 1

Related Questions