4

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.

  • 1
    Do you mean index like a MySQL index or a Magento indexer? – Robbie Averill Jun 23 '16 at 12:47
  • A Magento Indexer. – user36446 Jun 23 '16 at 12:47
  • You should take a look at this question: magento.stackexchange.com/questions/28803/… – Alexweb Jun 23 '16 at 13:02
  • @Alexweb, this link is listed as allready been reviewed. – Akif Jun 23 '16 at 13:13
  • @Alexweb I've already taken a look at that question. It explains the coding part of the indexer but there is little explanation about the actual implementation of it. – user36446 Jun 23 '16 at 13:18
4
+50

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

  • Could you expand upon what the code should be/do at //here goes the code? Or provide a working example. – user36446 Jun 23 '16 at 15:44
  • Your requirement is really custom. I do not know how tour custom grid or module is built. – Marius Jun 23 '16 at 16:12
  • 1
    What is the general idea the code does at that point? If I wanted to index a single string would there be any code there? What would it do at that point in that case? – user36446 Jun 24 '16 at 9:37
  • 2
    @Kevin''KIPdeKIP''vanRijn. IN the reindexAll method you have to retrieve your sales for the last 90 days and process the orders according to your needs. I assume you have to calculate some values and store them somewhere. But I'm not familiar with your needs and your table schemas, or where you need these values stored. – Marius Jun 27 '16 at 10:47

Your Answer

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