@raven_corwin
To print an SQL query in Magento 2, you can use the following methods:
1 2 3 4 5 6 7 |
$profiler = Mage::getSingleton('core/resource')->getConnection('core_read')->getProfiler(); $profiler->setEnabled(true); // Perform the SQL query $query = $profiler->getLastQueryProfile(); echo $query->getQuery(); |
1 2 3 |
$connection = Mage::getSingleton('core/resource')->getConnection('core_read'); $sql = $connection->select()->from('table_name', array('*')); echo $sql->__toString(); |
1 2 3 |
$collection = Mage::getModel('catalog/product')->getCollection(); $sql = $collection->getSelect(); Mage::log($sql->__toString()); |
Note that the Mage::log()
function will write the SQL query to the Magento log file, which is typically located at var/log/system.log
.
I hope this helps! Let me know if you have any questions.