php Magento getModel('catalog/product')->getCollection() with addAttributeToSelect('*') 不返回描述

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26614997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:58:28  来源:igfitidea点击:

Magento getModel('catalog/product')->getCollection() with addAttributeToSelect('*') not returning description

phpmagento

提问by erincerol

I have below code to get all products data from magento but it is not returning me the description field. I'm only getting short_description. Actually,its missing several fields in addition to description. Anyway here is the code:

我有以下代码可以从 magento 获取所有产品数据,但它没有返回描述字段。我只得到short_description。实际上,除了描述之外,它还缺少几个字段。无论如何这里是代码:

$collection = Mage::getModel('catalog/product')->getCollection()
            ->joinField(
                    'qty', 'cataloginventory/stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left'
            )
            ->addAttributeToFilter('status', 1) // enabled
            ->addUrlRewrite()
            ->addPriceData()
            ->addStoreFilter($store_id)
            ->addAttributeToSelect('*');
    Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($collection);
    Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);
    $collection->setOrder('sku', 'desc');

This creates the following query when I var_dump it:

当我 var_dump 时,这会创建以下查询:

SELECT 1 AS `status`, `e`.`entity_id`, `e`.`type_id`, `e`.`attribute_set_id`, `at_qty`.`qty`, `price_index`.`price`, `price_index`.`tax_class_id`, `price_index`.`final_price`, IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price, price_index.tier_price), price_index.min_price) AS `minimal_price`, `price_index`.`min_price`, `price_index`.`max_price`, `price_index`.`tier_price`, `e`.`entity_id`, `e`.`attribute_set_id`, `e`.`type_id`, `e`.`allow_open_amount`, `e`.`cost`, `e`.`created_at`, `e`.`email_template`, `e`.`enable_googlecheckout`, `e`.`giftcard_amounts`, `e`.`giftcard_type`, `e`.`gift_message_available`, `e`.`gift_wrapping_available`, `e`.`gift_wrapping_price`, `e`.`has_options`, `e`.`image_label`, `e`.`is_recurring`, `e`.`is_redeemable`, `e`.`lifetime`, `e`.`links_exist`, `e`.`links_purchased_separately`, `e`.`links_title`, `e`.`msrp`, `e`.`msrp_display_actual_price_type`, `e`.`msrp_enabled`, `e`.`name`, `e`.`news_from_date`, `e`.`news_to_date`, `e`.`open_amount_max`, `e`.`open_amount_min`, `e`.`price`, `e`.`price_type`, `e`.`price_view`, `e`.`recurring_profile`, `e`.`required_options`, `e`.`shipment_type`, `e`.`short_description`, `e`.`sku`, `e`.`sku_type`, `e`.`small_image`, `e`.`small_image_label`, `e`.`special_from_date`, `e`.`special_price`, `e`.`special_to_date`, `e`.`tax_class_id`, `e`.`thumbnail`, `e`.`thumbnail_label`, `e`.`updated_at`, `e`.`url_key`, `e`.`url_path`, `e`.`use_config_email_template`, `e`.`use_config_is_redeemable`, `e`.`use_config_lifetime`, `e`.`visibility`, `e`.`weight`, `e`.`weight_type`, `e`.`brand`, `e`.`brand_value`, `e`.`new_in`, `e`.`new_in_value`, `e`.`amconf_simple_price`, `e`.`volume_weight`, `e`.`product_3rd_type`, `e`.`product_3rd_type_value`, `e`.`product_sub_type`, `e`.`product_sub_type_value`, `e`.`product_type`, `e`.`product_type_value`, `e`.`parent_sku`, `at_inventory_in_stock`.`is_in_stock` AS `inventory_in_stock` FROM `catalog_product_flat_1` AS `e`
 LEFT JOIN `cataloginventory_stock_item` AS `at_qty` ON (at_qty.`product_id`=e.entity_id) AND (at_qty.stock_id=1)
 INNER JOIN `catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND price_index.website_id = '1' AND price_index.customer_group_id = 0
 INNER JOIN `cataloginventory_stock_item` AS `at_inventory_in_stock` ON (at_inventory_in_stock.`product_id`=e.entity_id) AND ((at_inventory_in_stock.use_config_manage_stock = 0 AND at_inventory_in_stock.manage_stock=1 AND at_inventory_in_stock.is_in_stock=1) OR (at_inventory_in_stock.use_config_manage_stock = 0 AND at_inventory_in_stock.manage_stock=0) OR (at_inventory_in_stock.use_config_manage_stock = 1 AND at_inventory_in_stock.is_in_stock=1))

So there is only short_description but I have no idea why description is not there.

所以只有 short_description 但我不知道为什么没有描述。

I know I can do

我知道我能做到

Mage::getModel('catalog/product')->load($product->getId())

But I don't want to query the DB lots of times. Is there way to work around this or get those fields when I do getCollection()?

但我不想多次查询数据库。当我执行 getCollection() 时,有没有办法解决这个问题或获取这些字段?

Update: It seems my main issue is, I'm only getting required fields when I run the first code. In section where I get $collectionI want to include additional fields in the description page of the product. What would be the code for that without doing load for each single product?

更新:看来我的主要问题是,当我运行第一个代码时,我只得到必填字段。在我得到的部分中,我$collection想在产品的描述页面中包含其他字段。如果不对每个单个产品进行加载,那么代码是什么?

回答by Elavarasan

Magento doesn't load initially all attributes when using the model collection like this,

当使用这样的模型集合时,Magento 最初不会加载所有属性,

$collections = Mage::getModel('catalog/product')->getCollection(); 

It's select the table

这是选择表

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

Magento using the orm tables. If we dig more in this,

Magento 使用 orm 表。如果我们在这方面深入挖掘,

foreach($collections as $collection) {
    echo $collection->getName();
}

Now you can see the additional quires will be running.

现在您可以看到额外的查询将运行。

For more details please go here

欲了解更多详情,请到这里

But you can get all product detail collection from flat table without querying multiple time to database . For that you need to enable the flat in admin-> system->configuration ->catalog->catalog-> Front end->Here you can see the options to enable flat catalog

但是您可以从平面表中获取所有产品详细信息集合,而无需多次查询数据库。为此,您需要在admin-> system->configuration ->catalog->catalog-> Front end->此处启用平面,您可以在此处看到启用平面目录的选项

Use Flat Catalog Category -> Enable
Use Flat Catalog Product  -> Enable

For more details about flat catalog please go here.

有关平面目录的更多详细信息,请访问此处

So now you see the query now it will like,

所以现在你会看到查询,它会喜欢,

SELECT 1 ASstatus, e.entity_id, e.type_id, e.attribute_set_id, e.entity_id, e.attribute_set_id, e.type_id, e.allow_open_amount, e.cost, e.created_at, e.email_template, e.enable_googlecheckout, e.giftcard_amounts, e.giftcard_type, e.gift_message_available, e.gift_wrapping_available, e.gift_wrapping_price, e.has_options, e.image_label, e.is_recurring, e.is_redeemable, e.lifetime, e.links_exist, e.links_purchased_separately, e.links_title, e.msrp, e.msrp_display_actual_price_type, e.msrp_enabled, e.name, e.news_from_date, e.news_to_date, e.open_amount_max, e.open_amount_min, e.price, e.price_type, e.price_view, e.recurring_profile, e.required_options, e.shipment_type, e.short_description, e.sku, e.sku_type, e.small_image, e.small_image_label, e.special_from_date, e.special_price, e.special_to_date, e.tax_class_id, e.thumbnail, e.thumbnail_label, e.updated_at, e.url_key, e.url_path, e.use_config_email_template, e.use_config_is_redeemable, e.use_config_lifetime, e.visibility, e.weight, e.weight_type, e.necklace_length, e.necklace_length_valueFROM catalog_product_flat_1AS e

SELECT 1 ASstatus, e. entity_id, e. type_id, e. attribute_set_id, e. entity_id, e. attribute_set_id, e. type_id, e. allow_open_amount, e. cost, e. created_at, e. email_template, e. enable_googlecheckout, e. giftcard_amounts, e. giftcard_type, e. gift_message_available, e. gift_wrapping_available, e. gift_wrapping_price, e. has_options, e. image_label, e. is_recurring, e. is_redeemable, e. lifetime, e. links_exist, e. links_purchased_separately, e. links_title, e. msrp, e. msrp_display_actual_price_type, e. msrp_enabled, e. name, e. news_from_date,e. news_to_date, e. open_amount_max, e. open_amount_min, e. price, e. price_type, e. price_view, e. recurring_profile, e. required_options, e. shipment_type, e. short_description, e. sku, e. sku_type, e. small_image, e. small_image_label, e. special_from_date, e. special_price, e. special_to_date, e. tax_class_id, e. thumbnail, e. thumbnail_label, e. updated_at, e. url_key, e. url_path, e. use_config_email_template, e. use_config_is_redeemable, e. use_config_lifetime, e. visibility, e. weight, e. weight_type, e.necklace_length, e. necklace_length_valuecatalog_product_flat_1ASe

But remember some methods are not available in flat catalog and also if it is disabled.

但请记住,某些方法在平面目录中不可用,并且如果它被禁用。

Function Name                   Flat Catalog Enabled    Flat Disabled
setLoadProductCount($flag)       No                      Yes
setProductStoreId($storeId)      No                      Yes
addParentPathFilter($parent)     Yes                     No
addStoreFilter()                 Yes                     No
addSortedField($sorted)          Yes                     No

回答by Keyur Shah

Please try setting product attribute for descriptionfield "Used in Product Listing" to "YES". That will solve your problem and avoid individual product model load.

请尝试将description字段“ Used in Product Listing”的产品属性设置为“ YES”。这将解决您的问题并避免单个产品模型负载。

But make sure after change this attribute you have to re index data.

但是请确保在更改此属性后您必须重新索引数据。