I'm able to fetch data from custom form but not able to put it in database table. Here I post details of model and controller
SimpleMagento\Custom\Model\Name.php
class Name extends \Magento\Framework\Model\AbstractModel {
public function _construct()
{
$this->_init('SimpleMagento\Custom\Model\ResourceModel\Name');
}
SimpleMagento\Custom\Model\ResourceModel\Name.php
class Nameextends\Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
public function _construct()
{
$this->_init("new_info","entity_id");
}
}
SimpleMagento\Custom\Model\ResourceModel\Name\Collection.php
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
public function _construct()
{
$this->_init("SimpleMagento\Custom\Model\Name","SimpleMagento\Custom\Model\ResourceModel\Name");
}
}
Controller\Test.php
$model = $this->_name->create();
$entity = $this->getRequest()->getParams();
$model->addData([
"entity_id" => $entity['entity_id'],
"customer_id" => $entity['customer_id'],
"position" => $entity['position'],
"department" => $entity['department']
]);
$saveData = $model->save();
if($saveData)
{
$this->messageManager->addSuccess('Record Inserted...!');
}
else
{
$this->messageManager->addSuccess('Record Inserted...!');
}
Table Structure -
class InstallSchema implements InstallSchemaInterface
{
public function install(SchemaSetupInterface $setup,
ModuleContextInterface $context)
{
$setup->startSetup();
$name = $setup->getTable('new_info');
// Create your table now
$table = $setup->getConnection()->newTable($name
)->addColumn(
'entity_id',
Table::TYPE_INTEGER,
null,
[
'identity' => true,
'nullable' => false,
'primary' => true
],
'ID'
)->addColumn(
'customer_id',
Table::TYPE_INTEGER,
255,
[
'nullable' => false
],
'Customer ID'
)->addColumn(
'position',
Table::TYPE_TEXT,
255,
[
'nullable' => false,
'default' => ''
],
'Position of employee'
)->addColumn(
'department',
Table::TYPE_TEXT,
255,
[
'nullable' => false,
'default' => ''
],
'Department of employee'
)->setComment("Custom_table");
$setup->getConnection()->createTable($table);
$setup->endSetup();
}