Magento – How to print collection thesql query in magento

debuggingmagento

Let's say I have a collection like:

$products = Mage::getModel('catalog/product')
            ->getCollection()
            ...
            ->load();

How do I print the actual MySQL code that gets executed?

Best Solution

You can always view your sql query at a certain point by echoing getSelect as shown:

$products = Mage::getModel('catalog/product')
        ->getCollection();
echo $products->getSelect();

To change query parameters you want to check out methods like:

$products->addAttributeToSelect('someattribute');
$products->addAttributeToFilter('someattribute', array('eq'=>'1'));