Magento 1.9 – Implementing a Custom Indexer in an Extension

backendextensionsindexerindexingmagento-1.9

I'm working on adding an additional column to a grid in an existing backend extension. I need to add an indexer to to this extension indexing the health (the amount of sells) of (roughly) the last 3 months. (90 days.) I'm splitting the health per month. (So, a field each for 1-30, 31-60 and 61-90 days ago.)

Having basically zero knowledge of Magento indexers I decided to create a simple featureless extension (a functioning adminhtml page and nothing more) and attempt to implement an indexer on that to learn how indexing works.

I've already looked at a few guides trying to figure out how to do this but none of them have fully functioning examples.

They include code needed to create an indexer without detailing how to implement it.

How would I create an indexer, implement it and use it for this kind of data? What type of index do I need to index this information?

I'm looking for an explanation of Magento indexes and a full implementation. Being able to get an index up and running with this kind of data in either extension works for me.

Best Answer

In order to declare an index you need to add this to config.xml inside the global tag:

    <index>
        <indexer>
            <some_unique_index_name_here>
                <model>[module]/indexer_something</model>
            </some_unique_index_name_here>
        </indexer>
    </index>

The model tag in the code above contains the alias for your indexer model. It's similar to the one you use for Mage::getModel(...).

Then you need to create the class referenced in the model tag above:

//you need to extend Mage_Index_Model_Indexer_Abstract class
class [Namespace]_[Module]_Model_Indexer_Something extends Mage_Index_Model_Indexer_Abstract 
{
    //you need to add the following 2 methods `_registerEvent` and `_processEvent`. Let''s just make them do nothing for now.
    protected function _registerEvent(Mage_Index_Model_Event $event) 
    {
        return $this;
    }
    protected function _processEvent(Mage_Index_Model_Event $event) 
    {
        return $this;
    }
    //this is the index name that shows up in indexes section
    public function getName() 
    {
        return Mage::helper('[module]')->__('Your index name');
    }
    //this is the index description that shows up in indexes section
    public function getDescription() 
    {
        return Mage::helper('[module]')->__('Your index description');
    }
    //magic happens here
    public function reindexAll() 
    {
        //here goes the code you need for reindexing
        return $this;
    }
}

If your index needs to run only from the indexes screen or command line then you are set.
If you need your index to be triggered at different events it gets more complicated.
You need to implement the methods that I left blank above _registerEvent and _processEvent.
But these are really specific depending on your events.
You can take a look at these methods from the core indexers to see what happens.
The flat product indexer for example: https://github.com/OpenMage/magento-mirror/blob/magento-1.9/app/code/core/Mage/Catalog/Model/Product/Indexer/Flat.php

Related Topic