I have created a custom table in database of magento 2. I want to insert some data into this custom table. I want to pass this data from controller to the custom table in magento 2.4. Please can any one guide how can i do that.
4 Answers 4
<?php
namespace YourNameSpace\YourModule\Helper;
use Magento\Framework\App\ResourceConnection;
class Data extends AbstractHelper
{
private $connection;
public function __construct(
ResourceConnection $resourceConnection
)
{
$this->connection = $resourceConnection->getConnection();
}
public function saveData()
{
$bind = [
'column1' => $column1,
'column2' => $column2,
'column3' => $column3,
'column4' => $column14,
];
$this->connection->insert('your_table_name', $bind);
}
}
You need to create models
=>Resource Model ( for connect with database)
=>Model ( get and save data)
=>Collection
You can get reference from Devdoc or
https://www.codilar.com/magento-2-models-resource-models-and-collections
https://magestore-product.gitlab.io/devdocs/internship-documentation/magento/model/
You can insert data into database using one of the below two methods
Method 1 : https://bsscommerce.com/confluence/how-to-create-insert-data-into-the-table-in-magento-2/
Method 2 :
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('tbl_name'); //gives table name with prefix
$sql = "Insert Into " . $tableName . " (id, name) Values ('','XYZ')";
$connection->query($sql);
You have to create Model, ResourceModel for that. And use that model to set and save the data.
1. Model Path app/code/vendor/module/Model/Custom.php
<?php
namespace vendor\module\Model;
use Magento\Framework\Model\AbstractModel;
use vendor\module\Model\ResourceModel\Custom as
CustomResource;
class Custom extends AbstractModel{
protected function _construct()
{
$this->_init(CustomResource::class);
}
}
2. ResourceModel Path app/code/vendor/module/Model/ResourceModel/Custom.php
<?php
namespace vendor\module\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class Custom extends AbstractDb{
const MAINTABLE = 'custom_table';
const ID_FIELD_NAME = 'entity_id';
protected function _construct()
{
$this->_init(self::MAINTABLE,self::ID_FIELD_NAME);
}
}
2. Controller
In your controller (app/code/vendor/module/Controller/Insertion/Add.php)
<?php
namespace vendor\module\Controller\Insertion;
use vendor\module\Model\Resourcemodel\Custom as CustomResource;
use vendor\module\Model\CustomFactory;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
class Add extends Action{
private $customFactory;
private $customResource;
public function __construct(
Context $context,
CustomFactory $customFactory,
CustomResource $customResource
)
{
parent::__construct($context);
$this->customFactory=$customFactory;
$this->customResource=$customResource;
}
public function execute()
{
$data=$this->getRequest()->getParams();
$customModel=$this->customFactory->create();
$customModel->setData($data);
try{
$this->customResource->save($customModel);
$this->messageManager->addSuccessMessage('Data added successfully');
}catch(\Exception $e){
$this->messageManager->addErrorMessage('Data Not Added'.$e->getMessage());
}
$redirector= $this->resultRedirectFactory->create();
$redirector->setPath('frontname/controller/action'); //Your path
return $redirector;
}
}
In this way you can insert data to the table
Explore related questions
See similar questions with these tags.