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

查看:57
本文介绍了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');

获取用于查询单个产品的模式资源对象,并使用以下的任何一个

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中,每个Model对象都有一个名为"getCollection"的方法,该方法返回其对应的Collection Resource.

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

然后让我们用它来查看

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天全站免登陆