title: Magento - extending the Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection class
body: <p>OK, so this is leading on from another question I asked here recently. Basically, I want to extend the <strong>Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection</strong> class, so I can add some extra filters for the product collections that can be re-used throughout my store (such as best-selling). This is meant to replace the following code, which I currently use, which is in my template.phtml file:</p>
  
  <pre><code>$_bs_productCollection = Mage::getResourceModel('reports/product_collection')
  -&gt;addAttributeToSelect('name')
  -&gt;addAttributeToFilter('visibility', $visibility)
  -&gt;addOrderedQty()
  -&gt;setOrder('ordered_qty', 'desc')
  -&gt;setPageSize(6);
  $_bs_productCollection-&gt;load();
  </code></pre>
  
  <p>So, I set up my module, and it's loading (it shows in the admin/system/config/advanced). Folder structure as follows:</p>
  
  <pre><code>etc/modules/Samsmodule.xml
  local/Samsmodule
  local/Samsmodule/Catalog
  local/Samsmodule/Catalog/etc
     local/Samsmodule/Catalog/etc/config.xml
  local/Samsmodule/Catalog/Model
     local/Samsmodule/Catalog/Model/Resource/Eav/Mysql4/Product/Collection.php
  local/Samsmodule/Catalog/Helper (not sure if this is needed or not)
  </code></pre>
  
  <p>My <strong>Samsmodule.xml</strong> is:</p>
  
  <pre><code>&lt;config&gt;
  &lt;modules&gt;
      &lt;Samsmodule_Catalog&gt;
          &lt;active&gt;true&lt;/active&gt;
          &lt;codePool&gt;local&lt;/codePool&gt;
      &lt;/Samsmodule_Catalog&gt;
  &lt;/modules&gt;
  &lt;/config&gt;
  </code></pre>
  
  <p>My <strong>config.xml</strong> is:</p>
  
  <pre><code>&lt;config&gt;
  &lt;modules&gt;
      &lt;Samsmodule_Catalog&gt;
          &lt;version&gt;0.1.0&lt;/version&gt;
      &lt;/Samsmodule_Catalog&gt;
  &lt;/modules&gt;
  &lt;global&gt;
      &lt;models&gt;
          &lt;catalog_resource_eav_mysql4&gt;
              &lt;rewrite&gt;
                  &lt;product_collection&gt;Samsmodule_Catalog_Model_Resource_Eav_Mysql4_Product_Collection&lt;/product_collection&gt;
              &lt;/rewrite&gt;
          &lt;/catalog_resource_eav_mysql4&gt;
      &lt;/models&gt;
  &lt;/global&gt;
  &lt;/config&gt;
  </code></pre>
  
  <p>And my <strong>Collection.php</strong> is:</p>
  
  <pre><code>&lt;?php
  
  class Samsmodule_Catalog_Model_Resource_Eav_Mysql4_Product_Collection extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
  
  {
  public function filterbyBestSelling($attribute,$visibility,$_category,$no_of_items)
  {
      $this-&gt;addAttributeToSelect($attribute)-&gt;addOrderedQty()-&gt;setOrder('ordered_qty', 'desc')-&gt;addAttributeToFilter('visibility', $visibility)-&gt;addCategoryFilter($_category)-&gt;setPageSize($no_of_items);
      return $this;
  }
  }
  </code></pre>
  
  <p>And then from my template.phtml I call it like so:</p>
  
  <pre><code>$_bs_productCollection = Mage::getResourceModel('reports/product_collection')
  -&gt;filterbyBestSelling('name',$visibility,$_category,6);
  </code></pre>
  
  <p>But it's not working - what am I missing? If I just add the code from my <strong>Collection.php</strong> to the bottom of my core <strong>Collection.php</strong> file, and use the same call, it works fine.</p>