How to create Mysql Update query using Magento 2?

You can create update query using Magento 2 with Magento way.

You can write a custom update MySQL query in ResouceModel PHP file.

When you write any custom update query, You need to create a function in ResourceModel folder PHP file.
Continue reading “How to create Mysql Update query using Magento 2?”

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,
Continue reading “How to use insertOnDuplicate query in Magento 2.”

How to apply OR conditions to collection in Magento 2?

In Magento 2, You can apply OR conditions on collection. By default, When you work with collection, Your conditions will be AND.

AND Condition
Fetch collection of only those products whose sku is like 24MB% AND type_id is equal to simple.

Output:
SELECT e.* FROM catalog_product_entity AS e WHERE (e.sku LIKE ’24MB%’) AND (e.type_id = ‘simple’); Continue reading “How to apply OR conditions to collection in Magento 2?”