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");
 }
 }
asked Mar 18, 2020 at 5:57
5
  • you want to write you custom query am I right? Commented Mar 18, 2020 at 5:58
  • Yes @Jinesh but can we use like Zend format. Commented 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); Commented 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%'; Commented Mar 18, 2020 at 6:04
  • you can check my answer as well as check this link(magecomp.com/blog/…) Commented 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;
 }
}
answered Mar 18, 2020 at 6:08
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 Commented Mar 18, 2020 at 6:13
  • now check my answer. Commented Mar 18, 2020 at 6:17
  • Ok but here where I have to add place replace? Commented Mar 18, 2020 at 6:26
  • Here $where = ['entity_id = ?' => replace()]; Commented Mar 18, 2020 at 6:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.