I am trying to follow what "uguptu" is recommending here:


In particular, I am trying to run my own highly specific query on the sales/order model. I would like to derive my own collection from the sales/order_collection but need to know where to put that code he recommends. Do I put it in the model? If so, how do I modify config.xml for a tableless model?

1 Answer 1


I'm not sure whether I understand this question.

To make it correct, you derive a custom collection from the standard one. You either override _initSelect() and put your filter modifications and joins there, or you put it into some public method that can be executed for the collection in case you need a custom filtering.

So just put it in _initSelect()

Like this:

class Namespace_Module_Model_Resource_Order_Collection extends Mage_Sales_Model_Resource_Order_Collection
    public function _initSelect()
        $this->getSelect()->join( array('table_alias'=>$this->getTable('module/table_name')), 'main_table.foreign_id = table_alias.primary_key', array('table_alias.*'), 'schema_name_if_different');
        return parent::_initSelect();

But I highly recommend to use joinTable(), joinField, joinAttribute instead of directly working on the Zend_Db instance.

model and resource model in config.xml

Normally you define a prefix to use the models with Mage::getModel() and Mage::getSingleton(). If models write to the database, they have a resource model and a collection class. this is defined via the <resourceModel> node.

            <namespace_module> <!-- prefix -->

<!-- What happens with Mage::getModel('...')->getResourceModel() -->
<!-- What happens with Mage::getModel('...')->getCollection() -->

  • But what file do you put that model resource class in?
    – user2045
    Commented May 29, 2013 at 19:17
  • The above wasn't what I didn't know. What I need to know is, having done the above, what changes do I need to make to config.xml for my module to know about the above class and for that matter, where do I put that class? Somewhere in Models/Resources/...?
    – user2045
    Commented May 29, 2013 at 19:31
  • And the reason I wanted to make a custom collection is, I'm doing reporting on existing tables/models. My module doesn't really have any of it's own. I'm not adding another table, then doing CRUD stuff. I'm digging out data from existing tables/models.
    – user2045
    Commented May 29, 2013 at 19:35
  • You can place that code in Namespace/Module/Model/Resource/Order/Collection.php Commented May 29, 2013 at 20:47
  • edited the post, I hope this is enough to be understand. Commented May 29, 2013 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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