从已经加载的Magento模型中检索其他数据 [英] Retrieving additional data from already loaded Magento models

查看:89
本文介绍了从已经加载的Magento模型中检索其他数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,我会得到一个仅包含我需要的某些数据的模型,例如,一个catalog/product实例,其中不包含我可能需要使用的某些属性,例如大小,小部件编号,或腰围.

There are some occasions where I'm handed a model containing only some of the data I require, for example a catalog/product instances that doesn't contain certain attributes I may need to use, such as size, widget number, or waist measurement.

为减轻这种情况,我目前的选择是:

To alleviate this, my current options are:

  • 创建一个新块,并使用addAttributeToSelect($name)手动加载所需的属性.
  • 使用当前填充不足的模型中的ID(例如,使用Mage::getModel('catalog/product')->getId($product->getId()))将ID加载到模板中的整个模型.
  • Create a new block, and load the required attributes manually using addAttributeToSelect($name).
  • Loading the entire model in the template using the ID from the current, inadequately populated, model with, for example, Mage::getModel('catalog/product')->getId($product->getId()).

对于我的问题:是否有一种方法可以选择在调用->load()之后要加载到模型集合中的其他属性?另外,是否有一种方法可以对单个模型执行此操作?

To my question: is there a way I can pick additional attributes that I'd like to load in my model collection after ->load() has been called? Also, is there a method to do this on individual models?

推荐答案

所描述的正确和最安全的方法(但不是最好的方法-参见下文)-将再次加载产品.

The right and most safe approach (but not the best - see below) is described in question - it is to load product once again.

在加载产品之后,由于多种原因,尚无开发出的方法来添加更多属性:

There are no already developed methods to add more attributes, after the product is loaded, for several reasons:

1)在模型生命周期中,很多值都被计算并缓存在模型内部.因此,添加更多属性(例如价格)将更改模型的状态,但不会影响旨在返回这些属性值(例如getPrice())的几种方法的结果,但在内部进行一些附加的预处理并取决于先前计算的结果数据.

1) During Model lifetime a lot its values are calculated and cached inside the Model. Thereby adding more attributes (e.g. price) will change the Model's state, but won't affect results of several methods, that are designed to return these attributes values (e.g. getPrice()), but internally do some additional preprocessing and depend on previously calculated data.

2)模型的状态将不一致,因为某些方法将返回根据先前的空属性计算的已缓存和当前无效的值,而其他一些方法将返回未缓存的值.因此,使用这种模型将是不安全的,并且其属性将是不可预测的.

2) The Model's state will be inconsistent, as some methods will return cached and currently non-valid values, calculated on previous empty attribute, while some other methods will return non-cached values. So usage of such Model will be unsafe and its properties will be unpredictable.

3)支持此类重载的代码复杂度很高.

3) Complexity of code to support such reloading is quite big.

解决方案

1)第一个好的解决方案(虽然是最重的解决方案)是每次您的块/模型/帮助程序需要扩展属性集时再次加载产品.

1) The first good solution (although is the heaviest one) is to load product once again, every time your block/model/helper needs extended set of attributes in it.

2)更好的解决方案-加载具有所有其他属性的所有产品的新集合,只要您看到这些属性,将需要这些属性,而原始集合没有这些属性.

2) Better solution - is to load new collection with all products having all additional attributes, whenever you see, that these attributes will be required and original collection doesn't have them.

3)最佳解决方案-加载具有所有必需属性的原始产品集合.有时,集合实际上会加载具有可能属性子集的产品-主要是用于EAV优化的遗留代码(现在默认情况下已将平板"设置为打开",并且不需要此优化),或者当搜索引擎加载了集合时(例如Solr)在Magento EE中),默认情况下不会在其记录中存储所有属性.

3) The best solution - is to load original product collection with all required attributes. Sometimes collections really load products with subset of possible attributes - mainly it is a legacy code for EAV optimization (now flat tables are turned 'on' by default and this optimization is not needed) or possibly when collection is loaded by search engine (e.g. Solr in Magento EE) which, by default, doesn't store all the attributes in its records.

3.1)您可以在实例化该位置的原始集合中添加必需的属性-通过问题addAttributeToSelect($attributeNames)方法中提到的

3.1) You can add required attributes to the original collection at the place, where it is instantiated - via mentioned in question addAttributeToSelect($attributeNames) method

3.2)您可以将属性添加到自动填充到集合中的属性列表中.属性列表因模块而异,并且存储在不同的位置.一些在配置中,其他在数据库中.具体位置(配置表或数据库表),在何处添加用于自动填充的属性,具体取决于您的具体情况.

3.2) You can add your attributes to the list of attributes, automatically populated in a collection. Attributes lists differ from module to module, and they are stored in different places. Some are in config, others - in database. Concrete place (config or db table), where to add attributes for auto-population, depends on your concrete case.

4)有时,当您只需要属性值时,编写资源模型可能会更容易,更快捷,这将通过productIds和当前storeId范围直接从DB加载它们.然后,您可以冒险将它们设置为集合中产品的属性,或者安全地将它们设置为产品的myAdditionalAttribuesValuesArray属性,或用作映射到产品ID的独立数组.

4) Sometimes, when you need only attribute values, it maybe much easier and faster to write Resource Model, that will directly load them from DB by productIds and current storeId scope. Then you can take risk an set them as properties to Products in a collection or safely set them to Products as myAdditionalAttribuesValuesArray property or use as independent array, mapped to product ids.

这篇关于从已经加载的Magento模型中检索其他数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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