0

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();

}

  • can you please provide us more details, like which error is displaying? – Sagar Parikh SGR Dec 6 '19 at 5:55
  • No Errors but not even adding to database table – Akash Killedar Dec 6 '19 at 5:57
  • so I think it is printing data not inserted which will be in else part, right? – Sagar Parikh SGR Dec 6 '19 at 6:05
0

maybe its issue in your $saveData, you can var_dump that,

try this:

$model = $this->_name->create();
$entity = $this->getRequest()->getPostValue();
$model->addData([
    "entity_id" => $entity['entity_id'],
    "customer_id" => $entity['customer_id'],
    "position" => $entity['position'],
    "department" => $entity['department']
]);
try{
      $model->save();
      $this->messageManager->addSuccessMessage(__('Saved Successfully'));
   }
catch(\Exception $e){
      $this->messageManager->addErrorMessage($e->getMessage());
   }
| improve this answer | |
0

Try this it will work

Controller\Test.php

<?php
$model = $this->_name->create();
$entity = $this->getRequest()->getParams();

$model->setData([
    "entity_id" => $entity['entity_id'],
    "customer_id" => $entity['customer_id'],
    "position" => $entity['position'],
    "department" => $entity['department']
]);

// Or you can used this if above code not worked.
// $model->setEntityId($entity['entity_id']);
// $model->setCustomerId($entity['customer_id']);
// $model->setPosition($entity['position']);
// $model->setDepartment($entity['department']);

$saveData = $model->save();

if($saveData)
{
    $this->messageManager->addSuccess('Record Inserted...!');
}
else
{
    $this->messageManager->addError('Something went wrong..!!');
}
| improve this answer | |
  • Not working... I am getting Record Inserted but values ain't adding. – Akash Killedar Dec 6 '19 at 6:46
  • have you try both way ? – Hiren Dec 6 '19 at 6:50
  • please add database table structure in question – Hiren Dec 6 '19 at 6:51
  • yes Itried both – Akash Killedar Dec 6 '19 at 7:05
  • insert data manually in table and try to print data using controller – Hiren Dec 6 '19 at 7:18

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.