简体   繁体   English

需要帮助来了解Magento产品集合对象和目录/产品模型

[英]Need help understanding Magento product collection objects and the catalog/product model

I've inherited a Magento project which an old developer was in charge of. 我继承了一个由老开发商负责的Magento项目。 Having very limited Magento experience, I'd like to understand how product collections and the catalog/product model is working. 由于Magento的经验非常有限,我想了解产品系列和catalog/product模型的工作方式。

Specifically I'm wondering why the collection and product objects are so huge? 具体来说,我想知道为什么集合和产品对象如此之大?

I have code like this: 我有这样的代码:

<?php $_productCollection = $this->getLoadedProductCollection(); ?>
<?php foreach ($_productCollection as $_product): ?>
    <?php $_product = Mage::getModel( 'catalog/product' )->load( $_product->getId() ); ?>
    <?php print $this->getLayout()->createBlock('mymodule_name/category_products_item')->setProduct($_product)->toHtml() ?>
<?php endforeach ?>

When I do print_r($_productCollection) or print_r($_product) I get massive amounts of data, when all I really want is a few product attributes (ID, name, price, image URL, etc.). 当我执行print_r($_productCollection)print_r($_product)我得到大量数据,而我真正想要的只是一些产品属性(ID,名称,价格,图像URL等)。

I'm used to a custom relational database platform where, if I needed to use products, I'd just do something ultra-simple like SELECT id,name,price FROM products; 我习惯了自定义的关系数据库平台,如果需要使用产品,我会做一些非常简单的事情,例如SELECT id,name,price FROM products; ! I'm not at all sure how I'd achieve this in Magento. 我完全不确定如何在Magento中实现这一目标。 Do I create a custom model or something? 是否创建自定义模型或其他内容?

So can anyone please tell me: 所以任何人都可以告诉我:

1) Why are the above objects so huge? 1)为什么上述物体这么大?

2) If I only need a few product attributes (as above) then is there a way to limit the objects to only those attributes to reduce their size? 2)如果我只需要一些产品属性(如上所述),那么有没有办法将对象限制为仅那些属性以减小其大小?

3) Is there a performance or memory hit from using objects this way? 3)以这种方式使用对象是否会对性能或内存造成影响?

Many thanks! 非常感谢!

You have several ways to access data from magento : 您可以通过多种方式从magento访问数据:

  • using load() => it loads ALL the data (attributes) relative to a model. 使用load()=>它将加载相对于模型的所有数据(属性)。 It's very slow and performance-killer. 这是非常缓慢的性能杀手。 For a product, you should use a load() only on the product page (because all the data you'll use on that page is relative to a single object .. so you can load it the full way) 对于产品,您应该仅在产品页面上使用load()(因为您将在该页面上使用的所有数据都与单个对象有关。因此您可以完整地加载它)

  • using collection => when you need to retrieve a list of objects, you must use (at least) a collection. 使用collection =>时,当您需要检索对象列表时,必须(至少)使用一个集合。 It's up to you to decide of the attribute list you want to retrieve. 由您决定要检索的属性列表。 You can add attribute to select/filter and the collection will manage to do the SQL joins to EAV tables etc... in background 您可以添加属性以选择/过滤,集合将设法在后台对EAV表进行SQL连接等。

  • using custom SQL => collection can be slow when dealing with complex objects (a collection initialize many SQL joins to table you may not need)... the last method to access data from BDD is to create your own SQL in your ResourceModel 在处理复杂对象时,使用自定义SQL =>集合可能会很慢(一个集合会初始化许多您可能不需要的SQL连接到表)...从BDD访问数据的最后一种方法是在ResourceModel中创建自己的SQL

In the script you show, there is a huge error : you load a full model inside a foreach iterating on a collection. 在您显示的脚本中,存在一个巨大的错误:您在对集合进行迭代的foreach中加载完整的模型。 You must never do that, if you have to load() the product, I assume it's because you didn't find an attribute in the collection ? 您绝对不能这样做,如果您必须load()产品,我想是因为您没有在集合中找到属性? In that case you only have to modify the collection in order to retrieve the attribute... 在这种情况下,您只需要修改集合即可检索属性。

For a product for example, magento offer a way to automatically add (or remove) attributes to any product->collection you instantiate. 例如,对于某个产品,magento提供了一种向您实例化的任何product-> collection自动添加(或删除)属性的方法。 (see frontend/product/collection/attributes XML tag in the config.xml of Mage_Catalog) (请参阅Mage_Catalog的config.xml中的frontend / product / collection / attributes XML标签)

It's becouse magento use cache recursive OMG system and make relational database developers crazy. 这是因为magento使用缓存递归OMG系统并使关系数据库开发人员发疯。 I'm one of those. 我就是其中之一。 But, by my understanding i get it's better use Mage::getModel('model')->getCollection(), then some king of sql search with php becouse when Magento store starts it make a biggggggg cache schema to suply some info faster, it's a kind of indexring. 但是,据我了解,我最好使用Mage :: getModel('model')-> getCollection(),然后在Magento存储启动时使用php进行sql之王搜索,因为它使biggggggg缓存架构可以更快地提供一些信息,这是一种索引环。

So, if you want just few fields you can use .....->getCollection()->addAttributeToSelect('name'). 因此,如果只需要几个字段,则可以使用.....-> getCollection()-> addAttributeToSelect('name')。 For ex. 对于前。

Gl on your magento understanding. GL对您的magento理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Magento:需要根据目录价格规则获取产品集合 - Magento: Need to get product collection on catalog price rule applied Magento:需要获取产品选项的Catalog_Product_Option ID - Magento: Need to get Catalog_Product_Option ID for product options Magento目录产品价格 - Magento catalog product price 当预期使用Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection时,使用Mage_Catalog_Model_Resource_Product_Collection对象的Magento 1.9.0.1 - Magento 1.9.0.1 using Mage_Catalog_Model_Resource_Product_Collection object when Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection is expected Magento-从Mage_Catalog_Model_Resource_Product_Collection对象访问值 - Magento - Accessing values from an Mage_Catalog_Model_Resource_Product_Collection object 了解Magento标签块类型=“ catalog / product_list” name =“ home.catalog.product.list” - Understanding Magento tag block type=“catalog/product_list” name=“home.catalog.product.list” 需要帮助了解productAttribute()产品页面的方法 - Need Help Understanding productAttribute() Method for Product Pages 从Magento中的表catalog_product_bundle_selection获取模型 - Get model from table catalog_product_bundle_selection in Magento 在Magento的目录中获取每个产品的产品选项 - Get Product Options for each Product in the Catalog in Magento 目录视图上的Magento产品属性? - Magento product attributes on catalog view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM