Start your journey with us.

Embark on a journey of growth and client success. Sign up to access our cutting-edge Sales Pipeline Management and CRM platform.

Leaving So Soon! Tell Us What You Need!

Get Quick Consultation and a 360-degree Approach Tailored For Your Business.


Shweta

Written by

Shweta

January 30, 2020

Update Query without Using Model File in Magento 2

Get your free AI SEO audit

hare a few details and we’ll send a prioritized action plan


Today, Magento 2 is a popular CMS among eCommerce users. Be it any website, blog, or an online store, efficient use of database opens up the door of new opportunities. If we are digging into the technical jargon; interaction through MYSQL for collecting relevant data from the database, is quite important. It helps in effective website management. Magento 2 makes use of MYSQL query for tracking data of customers, products, sales, returns, etc. Many companies hire Magento 2 developer in order to join the competitive CMS system but they might feel it challenging when it comes to creating model files every time for filing a SQL query.

Before we move to know the Update query trick, read here what is Magento 2 exactly?

Magento 2 changes the way we think about eCommerce. Magento 2 is an eCommerce platform for all where anyone can create their business website. It is not just a CMS like WordPress but it is way vast then you can imagine. It is an open-source platform.

Those who are software engineers or expertise in this field may understand that for obtaining database information in Magento 2, a developer has to create a model file for any SQL query. This is where Magento 2 lacks behind as compared to the other CMS platforms. It is actually quite infeasible to create SQL queries every time when you need a piece of database information for your websites and online stores.

However, there is a workaround that lets you create SQL queries without the model file. This blog explains to you how you can update query unlimited time without using the model file.

Step 1 – Create an “Index.php” File

First of all, you need to create a file in the extension folder. This is the path where you need to create a file i.e. – “index.php” is – app\code\Vendor\Extension\Controller\ Updatequery \. For expert guidance, you may hire Magento 2 developer India to get helped in the process.

Step 2 – Create The Code

When you have created the folder, you need to create a code to execute the operation.

Below is the Code –

“ <?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;

    }

}”

Step 3 – Execute the Code

Upon executing the above code, users would be able to create as many SQL queries as they want without the need of model file.

Creating a Magento 2 Model file every time is a headache and through the process explained above helps in better development of your CMS. If you find this blog useful for handling your Magento 2 CMS, share with your friends and spread the awareness for good.

arrow
Aresourcepool
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.