Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento - extending the Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection class

Tags:

magento

OK, so this is leading on from another question I asked here recently. Basically, I want to extend the Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection 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:

$_bs_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('name')
->addAttributeToFilter('visibility', $visibility)
->addOrderedQty()
->setOrder('ordered_qty', 'desc')
->setPageSize(6);
$_bs_productCollection->load();

So, I set up my module, and it's loading (it shows in the admin/system/config/advanced). Folder structure as follows:

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)

My Samsmodule.xml is:

<config>
<modules>
    <Samsmodule_Catalog>
        <active>true</active>
        <codePool>local</codePool>
    </Samsmodule_Catalog>
</modules>
</config>

My config.xml is:

<config>
<modules>
    <Samsmodule_Catalog>
        <version>0.1.0</version>
    </Samsmodule_Catalog>
</modules>
<global>
    <models>
        <catalog_resource_eav_mysql4>
            <rewrite>
                <product_collection>Samsmodule_Catalog_Model_Resource_Eav_Mysql4_Product_Collection</product_collection>
            </rewrite>
        </catalog_resource_eav_mysql4>
    </models>
</global>
</config>

And my Collection.php is:

<?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->addAttributeToSelect($attribute)->addOrderedQty()->setOrder('ordered_qty', 'desc')->addAttributeToFilter('visibility', $visibility)->addCategoryFilter($_category)->setPageSize($no_of_items);
    return $this;
}
}

And then from my template.phtml I call it like so:

$_bs_productCollection = Mage::getResourceModel('reports/product_collection')
->filterbyBestSelling('name',$visibility,$_category,6);

But it's not working - what am I missing? If I just add the code from my Collection.php to the bottom of my core Collection.php file, and use the same call, it works fine.

like image 464
Marlon Creative Avatar asked Oct 06 '10 22:10

Marlon Creative


1 Answers

(Didn't mean to leave you hanging in the other thread, but there's not a quick answer to this one that wouldn't either seem like magic or just confuse people further.)

You don't need to override a class unless you're going to change the behavior of an existing method. You just need to create a new class that extend the existing class. Here's how you do that.

Terminology For the Normal State of Things

  1. Models are the logical classes/objects that define the behavior of some "thing" (product, etc.) in Magento

  2. Models contain Model Resources. Model Resources are the classes that do the actual fetching of data from some datastore (mysql, etc). This is the Data Mapper pattern.

  3. Collections are objects with array like properties that query the database and return a group of Models. Somewhat confusingly, Collections are also Model Resources.

So, in the normal state of things, you might say something like

Mage::getModel('catalog/product')

to get a product model and the underlying system uses

Mage::getResourceModel('catalog/product');

to get the Mode Resource object that queries for a single product, and either of the following are used

Mage::getResourceModel('catalog/product_collection');
Mage::getModel('catalog/product')->getCollection();

to get the Collection object that queries for many models. In current versions of Magento each Model object has a method named "getCollection" which returns its corresponding Collection Resource.

Reports Off the Rails

Reports go a little sideways, but everything is still within the same universe as described above. It turns out there's no such model as a

Mage::getModel('reports/product');

But there is a collection

Mage::getResourceModel('reports/product_collection')

If you look at the class (which you'll be extending below), you'll see that the 'reports/product_collection' collection

class Mage_Reports_Model_Mysql4_Product_Collection extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection

extends the the base product collection class. In other words, the client programmer working on the reports section had the exact same though you did. "I want to add some methods to to Mage::getModelResource('catalog/product_collection'). They did this by extending the base class.

Shut Up, I Just Want this to Work

So, what you really want to do here is

  1. Create a new class Samsnamespace_Samscatalog_Model_Mysql4_Product_Collection that extends the base Mage_Reports_Model_Mysql4_Product_Collection collection class.

  2. Ensure that a call to Mage::getModelResource('samscatalog/product_collection') returns an instance of the above class by configurig our module to use Models and Model Resources.

We're also going to change you Module structure around a little bit to help ease naming confusion. I'm not a big fan of giving module folders the same names as core modules (i.e. "Catalog"), and the top level folder (after local/) is actually a Namespace, not a module folder. (A namespace may contain many modules)

We are not overriding the class. We are configuring a custom module under your namespace to use both Models and Model Resources. We're then defining a model resource that extends an existing PHP class already in the system. Overrides should only be used when you want to change the behavior of a particular method call. (Appologies for harping on this, but there's enough general confusion in the communiy about this that it's worth harping on over. and over. and over.)

First, we're going to create the module directory structure and files. We'll just need two

local/Samsnamespace/Samscatalog/etc/config.xml
local/Samsnamespace/Samscatalog/Model/Mysql4/Product/Collection.php

(and don't forget to enable the module in app/etc/modules. If you're not sure what that means, start reading)

The Collection.php file should contain

<?php
class Samsnamespace_Samscatalog_Model_Mysql4_Product_Collection extends Mage_Reports_Model_Mysql4_Product_Collection    
{
    /* your custom methods go here*/
}

And the config file should contain

<config>
    <modules>
        <Samsnamespace_Samscatalog>
            <version>0.1.0</version>
        </Samsnamespace_Samscatalog>
    </modules>
    <global>
        <models>
            <samscatalog>
                <class>Samsnamespace_Samscatalog_Model</class>
                <resourceModel>samscatalog_mysql4</resourceModel>
            </samscatalog>

            <samscatalog_mysql4>
                <class>Samsnamespace_Samscatalog_Model_Mysql4</class>
            </samscatalog_mysql4>
        </models>
    </global>
</config>

With these files in place and the module enabled, you should be able to call

$test = Mage::getResourceModel('samscatalog/product_collection');           
var_dump(get_class($test));

and your collection will be returned, and you can add methods to your heart's content.

What's Going On

This is mind bending, so you can stop reading if you want. It's also a rehash of concepts I've covered elsewhere.

When you say

Mage::getResourceModel('samscatalog/product_collection');

The underlying mage system codes says "ok, so this resource model"

samscatalog/product_collection

is part of the

samscatalog/product

model (not entirely true in this case, but it's what the system thinks).

So, since the resource model samscatalog/product_collection is part of the samscatalog/product model, let's look at the config at

global/models/samscatalog/resourceModel

To get the resource model URI of

samscatalog_mysql4

and then let's use that to look at the config at

global/models/samscatalog_mysql4/class

to get the base classname for all Resource Models that are a part of this Module. This ends up being

Samsnamespace_Samscatalog_Model_Mysql4

Which means the samscatalog/product_collection Resource Model is named

Samsnamespace_Samscatalog_Model_Mysql4_Product_Collection

and then its just Magento's standard auto-load which does a

include('Samsnamespace/Samscatalog/Model/Mysql4/Product/Collection.php');
like image 109
Alan Storm Avatar answered Oct 31 '22 03:10

Alan Storm