How to Create Update query Without Using Model File in Magento 2

How to create Update query Without Using Model File Magento 2

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.

app\code\Vendor\Extension\Controller\Deletequery\

After creating the folder, you need to apply the below code in order to perform the update operation.

<?php namespace Vendor\Extension\Controller\Updatequery; use Magento\Framework\App\ResourceConnection; use Magento\Framework\App\Action\Context; class Index extends \Magento\Framework\App\Action\Action { const QUOTE_TABLE = '[TABLE_NAME]'; private $resourceConnection; public function __construct( Context $context, ResourceConnection $resourceConnection) { $this->resourceConnection = $resourceConnection;

        return parent::__construct($context);
    }

    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! ?‍??‍?

Previous Article

Ecommerce Success Relies Heavily On Instagram Marketing

Next Article

How to Successfully Sell Your Products on Amazon and Stay Ahead of The Game

Write a Comment
  1. Shankar Bolla

    In the Above statement : $updatedRows=$connection->update($tableName, $data, $where); How to print the raw query ?

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨