What does Load mean in Magento Objects?
I m trying to learn coding a bit through Magento, and I have to admit that I'm a bit confused about this notion of object chaining in it.
In fact I don't understand when to do a load and when I can avoid it. For exemple:
$product = Mage::getModel('catalog/product')->load($item->getProductId());
I would like to get the info of product from a p开发者_StackOverflow社区roduct ID in this case; why do I need to load it? ($item
is the loop of all the products of an order)
And here I don't need to do any load:
$customer = $payment->getOrder()->getCustomer();
I'm sorry in advance for my stupid question: What does load do comparing to my second example? Thanks a lot and have a nice day,
Anselme
Behind the scenes a method like $payment->getOrder()
is effectively (after checking to see if it's already loaded) doing this:
return Mage::getModel('sales/order')->load($this->getOrderId());
// $this in this context is $payment
So a load is still needed to retrieve the relevant data from the database, the getOrder()
method is just a convenience. The load()
method itself returns it's class instance, which is why you can assign it to $product
in your first example. The getOrder()
and getCustomer()
methods don't return themselves, they return a different object, which is why $payment
is not assigned to $customer
in your second example.
The Mage::getModel()
method is only responsible for determining the correct class and creating a blank instance of it. Instead of a load you could instead set it's data with a setData()
call, passing a keyed array of values. All of the setters return their object, just like load()
does.
$customer = $payment->getOrder()->getCustomer();
It means the id of the customer is already present in the session, so you don't need to explicitly tell magento to load the customer.
In the products case, you have to tell magento the product id of the product you want to get details of.
精彩评论