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

}

3
  • can you please provide us more details, like which error is displaying? Commented Dec 6, 2019 at 5:55
  • No Errors but not even adding to database table Commented Dec 6, 2019 at 5:57
  • so I think it is printing data not inserted which will be in else part, right? Commented Dec 6, 2019 at 6:05

2 Answers 2

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());
   }
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..!!');
}
7
  • Not working... I am getting Record Inserted but values ain't adding. Commented Dec 6, 2019 at 6:46
  • have you try both way ? Commented Dec 6, 2019 at 6:50
  • please add database table structure in question Commented Dec 6, 2019 at 6:51
  • yes Itried both Commented Dec 6, 2019 at 7:05
  • insert data manually in table and try to print data using controller Commented Dec 6, 2019 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.