Skip to main content

TO Execute Multiple Raw Query In Magento 2

<?php

        /*
         *
         * TO Execute Multiple Raw Query In Magento 2
         *
         * */
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$conn = $resource->getConnection('write');
$sqlScript = file($sqlFile);
$query = '';
foreach ($sqlScript as $line) {
$startWith = substr(trim($line), 0 ,2);
$endWith = substr(trim($line), -1 ,1);
if (empty($line) || $startWith == '--' || $startWith == '/*' || $startWith == '//') continue;
$query = $query . $line;
if ($endWith == ';') {
$conn->query($query);
$query= '';
}
}
echo '<div class="success-response sql-import-response">SQL file imported successfully</div>';

Comments

Popular posts from this blog

Magento 2 generate custom log

the custom log file created using below code for Magento 2.4.2 before version use this :  $writer = new \Zend\Log\Writer\Stream (BP . '/var/log/custom.log' ); $logger = new \Zend\Log\Logger (); $logger -> addWriter ( $writer ); $logger -> info ( 'Custom message' ); $logger -> info ( print_r ( $object -> getData (), true )); for Magento 2.4.2 or after version use this :  $writer = new \Laminas\Log\Writer\Stream (BP . '/var/log/custom.log' ); $logger = new \Laminas\Log\Logger (); $logger -> addWriter ( $writer ); $logger -> info ( 'text message' ); $logger -> info ( print_r ( $object -> getData (), true )); for Magento 2.4.3 version use this $writer = new \Zend_Log_Writer_Stream (BP . '/var/log/custom.log' ); $logger = new \Zend_Log (); $logger -> addWriter ( $writer ); $logger -> info ( 'text message' ); $logger -> info ( print_r ( $object -> getData (), true )); Or you can try this : f...

Find Duplicate Url In Magento

Find Duplicate Product Url SELECT Count(*),value FROM catalog_product_entity_varchar v WHERE EXISTS (SELECT * FROM eav_attribute a WHERE attribute_code = "url_key" AND v.attribute_id = a.attribute_id AND EXISTS (SELECT * FROM eav_entity_type e WHERE entity_type_code = "catalog_product" AND a.entity_type_id = e.entity_type_id )) AND store_id = 0 GROUP BY v.value HAVING Count(*) > 1 Find Duplicate Category Url SELECT Count(*),value FROM catalog_category_entity_varchar v WHERE EXISTS (SELECT * FROM eav_attribute a WHERE attribute_code = "url_key" AND v.attribute_id = a.attribute_id AND EXISTS (SELECT * FROM eav_entity_type e WHERE entity_type_code = "catalog_cate...