Im trying to learn how to join two tables using this tutorial but I'm returning a sql error. In order to work what ive done wrong I would like to see the query being generated, how do I display it?

I've tried Zend_Debug::dump($collection); that doesn't gice me the query, what does?


2 Answers 2

echo $collection->getSelect();

The collection's query is handled by it's select object, and echo casts it to a string which renders nicely as SQL. In some situations you need to cast it manually, e.g.

Mage::log((string) $collection->getSelect());
  • This is the preferred method.
    – philwinkle
    Commented Oct 15, 2013 at 13:18
  • could more context be provided around how to use this? an example maybe
    – ahnbizcad
    Commented Oct 30, 2015 at 0:14
  • @ahnbizcad I'd like to but as long as the collection is a Varien_Data_Collection_Db type this should work, and almost all collections in Magento are. Commented Oct 31, 2015 at 13:41

You can get the used query by using $collection->load(true) , this will enable the $collection->printLogQuery(true,true) (See Varien_Data_Collection_Db::load()) and print the used query to both your brwoser as well as log it to the system.log.

Not the answer you're looking for? Browse other questions tagged or ask your own question.