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
Nagaraju Kasa
5,9518 gold badges59 silver badges116 bronze badges
-
you want to write you custom query am I right?Jinesh– Jinesh2020年03月18日 05:58:48 +00:00Commented Mar 18, 2020 at 5:58
-
Yes @Jinesh but can we use like Zend format.Nagaraju Kasa– Nagaraju Kasa2020年03月18日 06:00:13 +00:00Commented 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– Jinesh2020年03月18日 06:03:16 +00:00Commented 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%';Nagaraju Kasa– Nagaraju Kasa2020年03月18日 06:04:11 +00:00Commented Mar 18, 2020 at 6:04
-
you can check my answer as well as check this link(magecomp.com/blog/…)Jinesh– Jinesh2020年03月18日 06:11:21 +00:00Commented Mar 18, 2020 at 6:11
1 Answer 1
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
Jinesh
2661 gold badge2 silver badges11 bronze badges
-
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 meNagaraju Kasa– Nagaraju Kasa2020年03月18日 06:13:08 +00:00Commented Mar 18, 2020 at 6:13
-
-
Ok but here where I have to add place replace?Nagaraju Kasa– Nagaraju Kasa2020年03月18日 06:26:52 +00:00Commented Mar 18, 2020 at 6:26
-
Here $where = ['entity_id = ?' => replace()];Jinesh– Jinesh2020年03月18日 06:31:56 +00:00Commented Mar 18, 2020 at 6:31
Explore related questions
See similar questions with these tags.
default