How To

How to Create Update query Without Using Model File in 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! ?‍??‍?

Click to rate this post!
[Total: 10 Average: 4]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

Recent Posts

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

11 hours ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

3 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

6 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

6 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

7 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

1 week ago