Magento 1.9 SQL – How to Display Magento Query as a SQL String

magento-1.9querystringsql

It works beautifully if I'm trying to get collection but it won't for single product by SKU.
for example:

$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $_sku);
    echo $test =  $product->getSelect()->__toString();

How can I get sql for a single product can I?
or I must get collection with filter by sku then getSelect()….

… after a quick research …

        $products = Mage::getModel('catalog/product')
                    ->getCollection()
                    ->addAttributeToSelect('*')
                    ->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED))
                    ->addAttributeToFilter('sku',$_sku);

it does the job.

Best Answer

load() (or loadByAttribute()) would execute the query already - so you'll have to catch the query before it is executed. As you say, for example by using ->getSelect()->__toString() on your getCollection().

Some references on this topic:

Related Topic