For Any Website, One crucial necessity is the utilization of Database. Especially for any CMS, it is quite essential to interact with MYSQL with fetching and retrieving of data from the database. Taking about Ecommerce sites Database plays the most important role in managing that store. One of the most used Ecommerce platforms i.e. Magneto 2 is a CMS. In order to store different information like product data, customer data, etc. it utilizes MYSQL.
There are times when a developer wants to perform database operations like storing some data, modifying data or deleting it during coding. But there are certain coding standards in Magento. And according to those standards, a developer has to create a model file in order to perform any SQL query. It is one hectic dilemma in this near-perfect platform for an Ecommerce store.
But what if it was possible to perform those SQL queries without the use of model files. Believe me, it’s possible. And today’s blog is regarding the same. Here I’ll show you how to perform an update query. In the below code update is shown which shows you how to perform an update for any number of times without the use of a modal file. So, let’s start…??
Initially, in order to perform it, you need to create an “Index.php” file inside my extension folder at the path which is defined below.
After creating the folder, you need to apply the below code in order to perform the update operation.
class Index extends \Magento\Framework\App\Action\Action
const QUOTE_TABLE = '[TABLE_NAME]';
public function __construct(
$this->resourceConnection = $resourceConnection;
public function execute()
$connection = $this->resourceConnection->getConnection();
$data = ["field1"=>3,"field2"=>15]; // you can use as per your requirement
$id = 1;
$where = ['entity_id = ?' => (int)$id];
$tableName = $connection->getTableName(self::QUOTE_TABLE);
$updatedRows=$connection->update($tableName, $data, $where);
echo "Updated Rows : ".$updatedRows;
That’s it. With the help of this code, you can execute multiple SQL update operations without creating a model file. Without creating a standard Magento 2 Model file can be very helpful in your development phase.
lastly,?If This blog was helpful, then don’t forget to share it with your Magneto friends. Along with that if you are facing any difficulty or error during the implementation of this code, just let us know.
Happy Coding! ????