5

I have to load some products... The (my) traditional way to load a product is:

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

Now I have the problem, that some products can be loaded by this way - but some not.

I'm sure, there is no difference between those products... Simple, enabled, Catalag/Search, on Stock...

So - this doesn't work:

    $productId = '133110';
    $product = Mage::getModel('catalog/product')
        ->load($productId);
    $price = $product->getPrice();

But this works:

    $productId = '133110';
    $product = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToFilter('entity_id', $productId)
        ->addAttributeToSelect('price')
        ->getFirstItem();
    $price = $product->getPrice(); 

Why???

Amit Bera
  • 77,456
  • 20
  • 123
  • 237
Miss Magenta
  • 151
  • 1
  • 1
  • 4
  • What is the product's stock status? is it in stock? – Haris Apr 21 '15 at 10:52
  • what difference you get when you dump the data? try $price = $product['price']; for first option – Haris Apr 21 '15 at 11:00
  • There is another module, wich uses the event catalog_product_collection_load_before, filtering Mage_Catalog_Model_Resource_Product_Collection by an attribute. So it seems, that the first method is affected by this event, the second not. Hm... – Miss Magenta Apr 21 '15 at 11:02

2 Answers2

3

Your question contains answer. In table catalog_product_entity there is no field with price for a product. So, you can't use method get price. When you get a collection, you link catalog_product_entity with catalog_product_entity_decimal, from where it takes attribute for price. By the way, this collection will take only price for the first store, so you should add store to filter (by addStoreFilter()) if you use multiple stores. The other way - you can make your first code work, if you use flat tables for products. In this case you will use catalog_product_flat table, there field price is already included.

mr4eshir
  • 116
  • 7
  • I use the flat tables. I was wondering, why some products can be load by ->load($id), but some not. The reason is, that another module is filtering the collection and Mage::getModel('catalog/product')->getCollection() is not effected by this, but so Mage::getModel('catalog/product')->load() – Miss Magenta Apr 21 '15 at 11:16
  • Try to reindex products. – mr4eshir Apr 21 '15 at 11:17
  • Mage::getModel('catalog/product')->getCollection() collects data from multiple tables in DB. ->load($id), if flat is enabled, use only one flat table, and this table is filled only in reindex process. – mr4eshir Apr 21 '15 at 11:21
2

[Edited]

In a simple word, we can say

Mage::getModel('catalog/product')->load(1) is object of Magento product class Mage_Catalog_Product_Model.

Where ,$collection->getFirstItem() is object of Magento product collection class Mage_Catalog_Product_Model_Resource_Product_Collection or Mage_Catalog_Model_Resource_Category_Flat_Collection.

The difference between those two depends on magento flat setting

I try to describe my point of view in below

When use Product flat setting enable:

Table:

`getFirstItem()`,product  has been load from `catalog_product_flat_STOREID` table.

`load()`, product is load from  product master table `catalog_product_entity` with  join with `multiple models,modules`.

Attributes:

**getFirstitem:** You can only get `few product attributes values Whose setting is enable for Use in Product listing.

load():get all attributevalues of a particular product with inventory prices,tax etc in full details.`

Speedup:

**getFirstitem:** it more firster ,because of  `data is coming  from single table`.

`load()` surely slow  because `multiple eav tables join,multiple module join like tax,inventory etcs`

When disable product Flat setting:

Table:

`getFirstItem():` product has `been load from catalog_product_entity  table and join multiple eav,products eav using` 
function like `addAttributeToSelect,addAttributeToSort,addAttributeToFilter()` etc. 

`load()` same `beehive whenever flat is enable`

Attributes:

`getFirstitem:`You can get `only those attribute value whose are  select via addAttributeToSelect.,addUrlRewriteToResult() etc.`

`load():` get `all attribute values  of a particular product with inventory prices, tax etc in full details`.

Speedup:

`getFirstitem:` T`otaly depend on attribute selection and join product t related models`.

load() s`urely slow  because multiple eav tables join, multiple module join like tax, inventory etc`.

As per my point view ,it totally depends on attribute selection,filter etc of product. If your attribute value is filled collection then you need to use collection.

Otherwise use Product model load. Because of collection is not enable giving all data.

Amit Bera
  • 77,456
  • 20
  • 123
  • 237