Magento - 扩展 Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection 类 [英] Magento - extending the Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection class

查看:22
本文介绍了Magento - 扩展 Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是我最近在这里问的另一个问题的导引.基本上,我想扩展 Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection 类,这样我就可以为产品系列添加一些额外的过滤器,这些过滤器可以在我的商店中重复使用(例如畅销产品).这是为了替换我目前使用的以下代码,该代码位于我的 template.phtml 文件中:

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();

所以,我设置了我的模块,它正在加载(它显示在 admin/system/config/advanced 中).文件夹结构如下:

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)

我的 Samsmodule.xml 是:

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

我的 config.xml 是:

<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>

我的Collection.php是:

<?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;
}
}

然后从我的 template.phtml 中我这样称呼它:

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

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

但它不起作用 - 我错过了什么?如果我只是将 Collection.php 中的代码添加到我的核心 Collection.php 文件的底部,并使用相同的调用,它就可以正常工作.

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.

推荐答案

(并不是要让你在另一个线程中徘徊,但是对于这个问题,没有一个快速的答案,既不会看起来像魔术,也不会只是进一步混淆人们.)

(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.

  1. 模型是定义 Magento 中某些事物"(产品等)行为的逻辑类/对象

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

模型包含模型资源.模型资源是从某些数据存储(mysql 等)中实际获取数据的类.这是数据映射器模式.

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.

集合是具有类似数组属性的对象,这些属性查询数据库并返回一组模型.有点令人困惑的是,集合也是模型资源.

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');

获取查询单个产品的Mode Resource对象,并且使用以下任意一个

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();

获取查询多个模型的 Collection 对象.在 Magento 的当前版本中,每个模型对象都有一个名为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 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');

但是有一个集合

Mage::getResourceModel('reports/product_collection')

如果您查看类(您将在下面对其进行扩展),您会看到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

扩展基本产品集合类.换句话说,在报告部分工作的客户端程序员与您所做的完全相同.我想向 Mage::getModelResource('catalog/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.

所以,你在这里真正想做的是

So, what you really want to do here is

  1. 创建一个新类 Samsnamespace_Samscatalog_Model_Mysql4_Product_Collection,它扩展了基本的 Mage_Reports_Model_Mysql4_Product_Collection 集合类.

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

通过配置我们的模块以使用模型和模型资源,确保对 Mage::getModelResource('samscatalog/product_collection') 的调用返回上述类的实例.

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.

我们还将稍微改变您的模块结构,以帮助缓解命名混乱.我不太喜欢为模块文件夹提供与核心模块相同的名称(即目录"),并且顶级文件夹(在 local/之后)实际上是一个命名空间,而不是模块文件夹.(一个命名空间可能包含很多模块)

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)

我们不会覆盖类.我们正在您的命名空间下配置一个自定义模块以使用模型和模型资源.然后,我们定义了一个模型资源,它扩展了系统中已有的 PHP 类.仅当您想要更改特定方法调用的行为时才应使用覆盖.(请原谅我在这件事上喋喋不休,但社区中对此有足够的普遍混淆,值得一遍又一遍地重复.)

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

(不要忘记在 app/etc/modules 中启用该模块.如果您不确定这意味着什么,开始阅读)

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

Collection.php 文件应该包含

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

并且配置文件应该包含

<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.

这太费神了,所以如果你愿意,你可以停止阅读.这也是我在别处介绍的概念的重述.

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

当你说

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

底层法师系统代码说好的,所以这个资源模型"

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

samscatalog/product_collection

samscatalog/product

模型(在这种情况下不完全正确,但这是系统的想法).

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

因此,由于资源模型 samscatalog/product_collectionsamscatalog/product 模型的一部分,让我们看看

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

获取

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

这意味着 samscatalog/product_collection 资源模型被命名为

Which means the samscatalog/product_collection Resource Model is named

Samsnamespace_Samscatalog_Model_Mysql4_Product_Collection

然后它只是 Magento 的标准自动加载,它执行

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

include('Samsnamespace/Samscatalog/Model/Mysql4/Product/Collection.php');

这篇关于Magento - 扩展 Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆