Question

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:

<?php

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.

Was it helpful?

Solution

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

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top