How to Insert Multiple Records using ResourceConnection in Magento 2
If you work with Magento 2 and need to insert multiple records into a database table, the ResourceConnection class is a useful tool that simplifies the process. This class enables you to connect to a database and execute various database operations.
To learn how to use ResourceConnection to insert multiple records into a table in Magento, follow the step-by-step guide mentioned below.
Let's assume you have a custom module named CustomModule_YourModule. In this example we'll insert multiple records in your_table_name table.
Inject the ResourceConnection class
In your custom module, you can inject the ResourceConnection class into your PHP class constructor to establish a database connection. For example, in a custom model class:
saveCopyzoom_out_map
namespace CustomModule\YourModule\Model;
use Magento\Framework\App\ResourceConnection;
class YourModel
{
protected $resourceConnection;
public function __construct(ResourceConnection $resourceConnection)
{
$this->resourceConnection = $resourceConnection;
}
public function insertMultipleRecords(array $data)
{
$connection = $this->resourceConnection->getConnection();
$tableName = $connection->getTableName('your_table_name');
$data = [
['column1' => 'Value1', 'column2' => 'Value2'],
['column1' => 'Value3', 'column2' => 'Value4'],
];
$connection->insertMultiple($tableName, $data);
}
}
In the above code
$datashould be an array of associative arrays, where each associative array represents a record to be inserted.- Update your table name with the proper prefix with this
getTableName('your_table_name').
Replace 'column1', 'Value1', etc., with your actual data values.
That's it! You have now inserted multiple records into your custom database table using the ResourceConnection in Magento 2.
I hope this post helped you to find what you were looking for.
Bookmark it for your future reference. Do comment below if you have any other questions on that.
P.S. Do share this note with your team.
AI-Powered Recommended Articles
How to Run Direct SQL Query in Magento 2
Learn how to run direct SQL queries in Magento 2 for custom database operations and management.
How to Create Model, Resource Model, and Collection in Magento 2
Step-by-step guide to creating a model, ResourceModel, and collection in Magento 2 for custom data handling.
How to create JSON controller in Magento 2
Learn how to create a JSON controller in Magento for handling AJAX requests and returning JSON data.
How to Create Custom Table and Insert Sample Data in Magento 2
Create custom tables in Magento 2 and insert sample data for testing or customization purposes.
How to Add Dynamic Row on Store Configuration in Magento 2
Learn how to add dynamic rows to store configuration settings in Magento 2 for flexible content management.
How To Create Custom Attribute Set Programmatically In Magento 2
Learn how to create custom attribute sets programmatically in Magento 2 for flexible product management.