How to use insertOnDuplicate query in Magento 2.

Use of insertOnDuplicate query using Magento 2,  You need to create a Connection object first to run the query using ResourceConnection class.

When you insert any new row into a table if the row causes a duplicate in Primary key or UNIQUE index, throw an error in MySQL.

Base Definition of function:

Example,

The first argument is tableName
The second argument is the key-value pair of an array.
The third argument is optional for the table’s field of array. (update fields pairs)

Check for other Direct SQL Query in Magento 2

2 Replies to “How to use insertOnDuplicate query in Magento 2.”

  1. Rakesh, is this an example of a class or PHP code? if the class then “class” annotation is missing, if pure PHP code, then why there is a __construct()?

Leave a Reply