Magento产品系列选择所有属性 [英] Magento Product Collection Select all Attributes

查看:73
本文介绍了Magento产品系列选择所有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个模型,以选择特定的产品及其所有属性.

I'm trying to create a model that selects a specific product and all it's attributes.

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');

但是可悲的是,尽管我之前已经做过数百次了,但是我们有一个Magento的时刻...

But sadly although I have done this hundreds of times before, we're having a Magento moment...

echo $products->getSelect()->__toString();

显示-> addAttributeToSelect忽略了我.

reveals that ->addAttributeToSelect is ignoring me.

SELECT `e`.* FROM `catalog_product_entity` AS `e`

当然,以典型的Magento方式,不会有任何异常或错误.

Of course in typical Magento fashion there are no exceptions or errors of any kinds.

我在自己的Adminhtml Grid块中调用这些方法,禁用了缓存(即使我知道Magento在禁用缓存时仍然缓存了一些模型数据(我删除了var/cache).

I am calling these methods in my own Adminhtml Grid block, caching is disabled (even though I know Magento still caches some model data when caching is disabled(i deleted var/cache).

我什至做不到:

->addAttributeToSelect('name');

我真的开始对这个平台失去耐心了,我已经使用了4个月了,它似乎浪费了更多的时间.任何帮助都将挽救生命!

I'm really starting to loose my patience with this platform, I've been using it for 4 months and it seems to waste more time than it saves. Any help would be a life saver!

推荐答案

您需要深入研究Magento,以了解Magento ORM的工作原理(集合实际上起作用.

You need to dig more into Magento to understand how Magento ORM works (what's with the EAV structure and how collections actually work).

为了获得所有产品属性,这是正确的:

In order to get all the product attributes this is correct:

$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');

您所缺少的是,当您这样做时,Magento不会为您提供与馆藏相关的所有查询:

What you are missing is the fact that Magento will not give you all the related queries for a collection when you do:

echo $collection->getSelect()//note: you don't need __toString(), read about OOP in PHP5

运行代码以仅获取集合时,Magento将仅运行以下查询:

When you run the code just to get the collection Magento will run only the following query:

SELECT `e`.* FROM `catalog_product_entity` AS `e`

当您开始遍历集合时:

$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
foreach($collection as $p) {
    echo $p->getName()."\n";//you can use in here any getCustomAttribute method
}

Magento将运行其他查询以获取所有属性和值:

Magento will run additional queries to get all the attributes and values:

SELECT `main_table`.`entity_type_id`, `main_table`.`attribute_code`, `main_table`.`attribute_model`, `main_table`.`backend_model`, `main_table`.`backend_type`, `main_table`.`backend_table`, `main_table`.`frontend_model`, `main_table`.`frontend_input`, `main_table`.`frontend_label`, `main_table`.`frontend_class`, `main_table`.`source_model`, `main_table`.`is_required`, `main_table`.`is_user_defined`, `main_table`.`default_value`, `main_table`.`is_unique`, `main_table`.`note`, `additional_table`.* FROM `eav_attribute` AS `main_table`
 INNER JOIN `catalog_eav_attribute` AS `additional_table` ON additional_table.attribute_id = main_table.attribute_id WHERE (main_table.entity_type_id = 10)

SELECT `t_d`.`entity_id`, `t_d`.`attribute_id`, `t_d`.`value` AS `default_value`, `t_s`.`value` AS `store_value`, IF(t_s.value_id IS NULL, t_d.value, t_s.value) AS `value` FROM `catalog_product_entity_text` AS `t_d`
 LEFT JOIN `catalog_product_entity_text` AS `t_s` ON t_s.attribute_id = t_d.attribute_id AND t_s.entity_id = t_d.entity_id AND t_s.store_id = 1 WHERE (t_d.entity_type_id = 10) AND (t_d.entity_id IN (16, 17, 18, 19, 20, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 41, 42, 44, 45, 46, 47, 48, 49, 51, 52, 53, 54, 74, 75, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166)) AND (t_d.attribute_id IN ('496', '507', '531', '97', '494', '509', '499', '492', '498', '104', '495', '497', '936', '500', '476', '506')) AND (t_d.store_id = 0) UNION ALL SELECT `t_d`.`entity_id`, `t_d`.`attribute_id`, `t_d`.`value` AS `default_value`, `t_s`.`value` AS `store_value`, IF(t_s.value_id IS NULL, t_d.value, t_s.value) AS `value` FROM `catalog_product_entity_int` AS `t_d`
 LEFT JOIN `catalog_product_entity_int` AS `t_s` ON t_s.attribute_id = t_d.attribute_id AND t_s.entity_id = t_d.entity_id AND t_s.store_id = 1 WHERE (t_d.entity_type_id = 10) AND (t_d.entity_id IN (16, 17, 18, 19, 20, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 41, 42, 44, 45, 46, 47, 48, 49, 51, 52, 53, 54, 74, 75, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166)) AND (t_d.attribute_id IN ('272', '510', '875', '877', '903', '501', '935', '952', '904', '102', '513', '110', '859', '862', '508', '863', '525', '502', '107', '860', '273', '274', '526', '861')) AND (t_d.store_id = 0) UNION ALL SELECT `t_d`.`entity_id`, `t_d`.`attribute_id`, `t_d`.`value` AS `default_value`, `t_s`.`value` AS `store_value`, IF(t_s.value_id IS NULL, t_d.value, t_s.value) AS `value` FROM `catalog_product_entity_decimal` AS `t_d`
 LEFT JOIN `catalog_product_entity_decimal` AS `t_s` ON t_s.attribute_id = t_d.attribute_id AND t_s.entity_id = t_d.entity_id AND t_s.store_id = 1 WHERE (t_d.entity_type_id = 10) AND (t_d.entity_id IN (16, 17, 18, 19, 20, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 41, 42, 44, 45, 46, 47, 48, 49, 51, 52, 53, 54, 74, 75, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166)) AND (t_d.attribute_id IN ('100', '960', '503', '943', '99', '567', '270', '101')) AND (t_d.store_id = 0) UNION ALL SELECT `t_d`.`entity_id`, `t_d`.`attribute_id`, `t_d`.`value` AS `default_value`, `t_s`.`value` AS `store_value`, IF(t_s.value_id IS NULL, t_d.value, t_s.value) AS `value` FROM `catalog_product_entity_varchar` AS `t_d`
 LEFT JOIN `catalog_product_entity_varchar` AS `t_s` ON t_s.attribute_id = t_d.attribute_id AND t_s.entity_id = t_d.entity_id AND t_s.store_id = 1 WHERE (t_d.entity_type_id = 10) AND (t_d.entity_id IN (16, 17, 18, 19, 20, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 41, 42, 44, 45, 46, 47, 48, 49, 51, 52, 53, 54, 74, 75, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166)) AND (t_d.attribute_id IN ('940', '571', '271', '562', '878', '106', '879', '906', '873', '703', '105', '103', '942', '941', '96', '836', '931', '874', '876', '905', '109', '880', '493', '881', '481', '570')) AND (t_d.store_id = 0) UNION ALL SELECT `t_d`.`entity_id`, `t_d`.`attribute_id`, `t_d`.`value` AS `default_value`, `t_s`.`value` AS `store_value`, IF(t_s.value_id IS NULL, t_d.value, t_s.value) AS `value` FROM `catalog_product_entity_datetime` AS `t_d`
 LEFT JOIN `catalog_product_entity_datetime` AS `t_s` ON t_s.attribute_id = t_d.attribute_id AND t_s.entity_id = t_d.entity_id AND t_s.store_id = 1 WHERE (t_d.entity_type_id = 10) AND (t_d.entity_id IN (16, 17, 18, 19, 20, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 41, 42, 44, 45, 46, 47, 48, 49, 51, 52, 53, 54, 74, 75, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166)) AND (t_d.attribute_id IN ('572', '573', '704', '705', '568', '569')) AND (t_d.store_id = 0)

要了解有关在各种上下文中运行哪些查询的更多信息,请创建一个空脚本,仅加载Mage::app()并使用集合,并在Varien_Db_Adapter_Pdo_Mysql中将$ _logAllQueries设置为true.其他信息:此处.

To understand more about which queries are run in various contexts create a empty script, load just Mage::app() and play with collections and set $_logAllQueries to true in Varien_Db_Adapter_Pdo_Mysql. Additional info: here.

这篇关于Magento产品系列选择所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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