1

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

http://www.magentocommerce.com/boards/viewthread/225234/

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?

user2045
  • 831
  • 3
  • 16
  • 29

1 Answers1

3

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.

<config>
    <global>
        <models>
            <namespace_module> <!-- prefix -->
                <class>Namespace_Module_Model</class>

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

            </namespace_module>
            <namespace_module_resource>
                <class>Idee_ShowCart_Model_Resource</class>
            </namespace_module_resource>
        </models>
    </global>
</config>
Fabian Blechschmidt
  • 35,388
  • 8
  • 75
  • 182
  • But what file do you put that model resource class in? – user2045 May 29 '13 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 May 29 '13 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 May 29 '13 at 19:35
  • You can place that code in Namespace/Module/Model/Resource/Order/Collection.php – Jeffrey L. Roberts May 29 '13 at 20:47
  • edited the post, I hope this is enough to be understand. – Fabian Blechschmidt May 29 '13 at 22:55