I've created a custom sort module that I'm attempting to debug. I'm curious if it is possible to display this information somewhere in my catalog pages (e.g. app/code/design/package/theme/template/catalog/product/list.phtml). Here is the observer portion of my custom module where I define the setOrder:


class MyExtension_CustomSort_Model_Observer
    public function onLoadCollection($observer)
        $collection = $observer->getCollection();

        $collection->setOrder('created_at', 'desc');
            ->setOrder('name', 'asc');

        return true;

I've tried echoing and using print_r to display my information but it seems I'm doing wrong. Any advice? The end result would be for me to see "created_at" and "name" somewhere near the top of page.

  • 1
    If you want to use the echo to debug if your extension is working or not, you can add a die(here) from your observer and you will see if it is being called. It will be a blank screen with "here" as the only content. – mbalparda Feb 18 '15 at 16:40
  • The reason your order statement is not showing is due to the order being added when the collection is actually loaded. See my answer for more details. – musicliftsme Feb 18 '15 at 16:55

Better yet, log the collection's query to a separate file. Or you can just do what mbalparda suggested, as well. The order statement gets incorporated only when the collection is loaded.

To examine the full query,

// After what you have posted..
$collection->load(true);   // ordering is set when the collection is loaded; see ref.
Mage::log($collection->getSelect()->__toString());  // prints the query to var/log/system.log

Note: Make sure to remove ->load(true) after you're done debugging.

Reference: https://stackoverflow.com/questions/9207240/get-collection-query-string

  • Ah, you were typing a similar answer as well ;) – Jisse Reitsma Feb 18 '15 at 16:53
  • Perfect! Thank you musicliftsme! Much more appropriate than what I was thinking. – sparecycle Feb 18 '15 at 19:36

I don't think a die() statement is a good pick. If you want to see how your setOrder behaves in the Magento query that is being generated for you, you can use the $collection->getSelect() method to get a dump of the SQL query. You can then echo the output of getSelect() to your screen or perhaps log it to the Magento system log using Mage::log('My query: '.$collection->getSelect());.

If you're having issues with the name and created_at attributes to appear in your query, you might need to add them to the query using $collection->addAttributeToSelect(array('name', 'created_at')).

Does this work for you?


Even better yet, force log the collection into whatever log page you want. Or you can just do what musicliftsme suggested, as well.

Mage::log($collection->getSelect()->__toString(),null,'thisfile.log',true); // prints the query to var/log/thisfile.log

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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