29

I am bit confused about using resource model and collection model. Sometimes when I see examples for loading products using specific attributes, in some examples, they use collection model and sometimes they use resource model.

Also when can I use specific method get... and set... along with table row name? Like getName, getId. I tried to use but unable get the values and it shows error: "calling undefined method in object"

$product = Mage::getModel('catalog/product')
    ->loadByAttribute('name', 'product_name');

echo $product->getName(); 
echo $product->getSku();

It shows "undefined method getSku()"

If I use var_dump($product), SKU shows in object, however unable to get the SKU using getSku()...

  • To be clear - $product in your example is an instance of Mage_Catalog_Model_Product, which is a "model" in Magento parlance. – benmarks Jun 8 '13 at 13:39
  • Yes, right. but I am confused when to use collection model and resources model and what methods are available to use. As I see collection models are available in Model>>collection folder of the module and resources are availble in resources model but when and which model need to use so I bit confused about it – MagentoBoy Jun 8 '13 at 15:43
  • I know, we need to use the available methods which are available in collection and outsources too. But I am difficulties to find to use correct model. And I also I am not able to get the result from the result set using "Get" methods means getName() using database table row name. It shows undefined method in object means I am accessing wrong model or method which i need to use so I am not congirm about which I need to use. – MagentoBoy Jun 8 '13 at 15:54
  • 1
    Thanks for asking this question..I was searching answer for this question long time.. +1.. – Elavarasan Jul 8 '14 at 14:14
63

Magento's CRUD models have three basic class types.

A "model" is what you'll most commonly use. This represents data of a particular type in a database agnostic way (product, category, CMS Page, Review, etc.)

A "resource model" is a class that, behind the scenes, does the actual fetching of data from Magento. Every model has a resource model that is used to load a single instance of a model from the database.

A "collection" is a class that loads an array like structure of multiple models based on a set of rules. Think of it like a SQL WHERE clause.

Confusing things somewhat is, behind the scenes, Magento also considers a collection to be a resource model. So there's collection resource models and individual resource models.

Generally speaking, when you want to load a specific item, you use a model. When you want to load a number of items, you use a collection resource model.

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

//loads many products where price is greater than 100
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*') //load all fields, see EAV below
->addAttributeToFilter('price', array('gt'=>'100'));

foreach($products as $product)
{
    var_dump($product->getSku());
}

One thing to keep in mind about loading via a collection resource model is each individual model's afterLoad method won't be called automatically, which means some data may not be loaded.

The individua resource model classes are usually not accessed directly. However, occasionally a specific model will have a resource model with a useful method on it. These you'll discover over time using the system.

Finally, there's one more distinction to be made. Magento's regular CRUD models have a special syntax for loading via something that's not the numeric ID. For example, here's how to load a CMS page model by title

$page = Mage::getModel('cms/page')->load('Home page','title');
var_dump($page->getData());

However, a few Magento models are EAV models. That stands for "entity attribute value", and is a fancy way of saying these models don't have a fixed set of fields. This includes product models, and category models. This feature is what lets you pick different attributes for different types of products. Each field on an EAV model is known as an attribute. The above syntax won't work with these models, instead you need to use the loadByAttribute method.

$product = Mage::getModel('catalog/product')->loadByAttribute('sku', 'n2610');

Regarding your specific problem, if you're getting an error with the specific text "method undefined", then something is very wrong with your system. Even if the getSku method doesn't exist, Magento has magic setter and getter methods which ensure a get call on any model will always return something, even if it's null.

What's more likely is you're getting an error like the following

Call to a member function getSku() on a non-object

This error is happening because the loadByAttribute method returns false if it can't load the object.

$product = Mage::getModel('catalog/product')->loadByAttribute('sku', 'this-is-not-a-sku');
var_dump($product); //prints false

Try something like this in your code.

$product = Mage::getModel('catalog/product')->loadByAttribute('sku', 'sku-value');
if($product)
{
    var_dump($product->getName());
    var_dump($product->getSku());
    var_dump($product->getData());
}
else
{
    var_dump("Could not load product");
}

In additional to the conditional, notice the getData() method. This will return an array of all the object's data as an array.

Hopefully this is enough to get you moving. Good luck!

  • @Alan.. Very nice description... I have read many of your posts on Magento :) .. I am so happy after seeing your reply to my post. I will move ahead with your information. If I will get any issue then , I hope i will get good support from this community. As I new in magento as well as pogramming so my quetion might be not so much conceptual but still I want to learn. I Really like magento framework alot :)..Thank you sir :) – MagentoBoy Jun 9 '13 at 5:45
  • I am sorry, I have one more question. If I call $product = Mage::getModel('catalog/product'); and then if I call getName() and getSku(). am I able to get the values if I iterate the $product in foreachloop?? or I have to call loadByAttribute() method?? – MagentoBoy Jun 9 '13 at 5:51
  • @MagentoBoy My apologies, but the language barrier appears to be masking your question. I'm not sure what you're asking. – Alan Storm Jun 9 '13 at 6:06
  • sir, I am sorry. In excitement i did not express my question clearly. But I tried and now I am able to get the lists of products and as now able to fetch the products using different attribute properly. :) and yes my question was, can I use get methods like getName(), getPrice(), getSku(), without calling addAttributeToSelect('*'), addAttributeToFilter() or loadByAttribute(); for ex. $product= Mage::getModel('catalog/product'); foreach($product as $p){ echo $p->getName()." ".$p->getSku();}... As get model will instantiate product class then we can access the access get and set methods. – MagentoBoy Jun 9 '13 at 13:31
  • 1
    @user8009 That error message can happen for myriad reasons. If you can't get FTP access, or reproduce the issue in a local/development version of the system you'll never solve the problem. – Alan Storm Aug 21 '14 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.