0

I am trying to add below SQL query in the form of Zend format using Magento connection object Can you please let me know the syntax of how to follow...

update eav_attribute_set set attribute_set_name = replace(attribute_set_name, '_src', '') where attribute_set_name LIKE '%_src%';

Here is my method

protected function execute(InputInterface $input, OutputInterface $output)
    {
        try {
            $connection = $this->resourceConnection->getConnection();
            $tableName = $connection->getTableName(self::ATTRIBUTE_SET_TABLE);

           //Add your logic here
             -----------------------------
             -----------------------------

        } catch (\Exception $e){
            $output->writeln("Something went wrong please check and try again");
        }

    }
5
  • you want to write you custom query am I right?
    – Jinesh
    Mar 18, 2020 at 5:58
  • Yes @Jinesh but can we use like Zend format. Mar 18, 2020 at 6:00
  • You can write query like this way $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $resource = $objectManager->get('Magento\Framework\App\ResourceConnection'); $connection = $resource->getConnection(); $tableName = $resource->getTableName('mytest'); $sql = "INSERT INTO " . $tableName . " (id, name, age, email) VALUES ('', 'Hello World', 88, '[email protected]')"; $connection->query($sql);
    – Jinesh
    Mar 18, 2020 at 6:03
  • update eav_attribute_set set attribute_set_name = replace(attribute_set_name, '_src', '') where attribute_set_name LIKE '%_src%'; Mar 18, 2020 at 6:04
  • you can check my answer as well as check this link(magecomp.com/blog/…)
    – Jinesh
    Mar 18, 2020 at 6:11

1 Answer 1

3

Try below code

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
                $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
                $connection = $resource->getConnection();


                //Update Data into table
                $sql = "update eav_attribute_set set attribute_set_name = replace(attribute_set_name, '_src', '') where attribute_set_name LIKE '%_src%';";
                $connection->query($sql);

Try another way :-

<?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;
    }
}
4
  • Thanks for your answer @jinesh but can we use same query in the form of zend format in Magento2? i am looking for that. Can u suggest me Mar 18, 2020 at 6:13
  • now check my answer.
    – Jinesh
    Mar 18, 2020 at 6:17
  • Ok but here where I have to add place replace? Mar 18, 2020 at 6:26
  • Here $where = ['entity_id = ?' => replace()];
    – Jinesh
    Mar 18, 2020 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.