Magento getModel ('catalog / product') & # 8594; getCollection () with addAttributeToSelect ('*') returning no description
I have below code to get all product data from magento but it doesn't return the description field to me. I am only getting short_description. In fact, it is missing several fields in addition to the description. Anyway, here's the code:
$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 request when I var_dump it:
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 the description is not there.
I know what I can do
Mage::getModel('catalog/product')->load($product->getId())
But I don't want to query the DB many times. Is there a way to get around this or get these fields when I get getCollection ()?
Update. My main problem seems to be: I only get required fields when I run the first code. In the section where I get $collection
, I want to add additional fields to the product description page. What would be the code to do this with no load for each individual product?
source to share
Magento does not initially load all attributes when using a model collection, for example
$collections = Mage::getModel('catalog/product')->getCollection();
Select a table
SELECT `e`.* FROM `catalog_product_entity` AS `e`
Magento using tables orm. If we dig more into this,
foreach($collections as $collection) {
echo $collection->getName();
}
Now you can see how additional requests will be executed.
For more information go here
But you can get the entire collection of product details from a flat table without querying a lot of time in the database. To do this, you need to enable the plane in admin-> system->configuration ->catalog->catalog-> Front end->
Here you can see options for enabling the flat catalog
Use Flat Catalog Category -> Enable
Use Flat Catalog Product -> Enable
For more information on the flat directory, please go here .
So now you see a query that he will like,
>SELECT 1 AS
status
,
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_value
FROM
catalog_product_flat_1
AS
e
But remember that some of the methods are not available in the flat directory, and also when 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
source to share