What does Load mean in Magento objects?

I'm trying to learn a little about coding through Magento, and I have to admit that I'm a little confused by this concept of a chain of objects in it.

Actually, I don’t understand when to do the load and when I can avoid it. For instance:

$product = Mage::getModel('catalog/product')->load($item->getProductId()); 

I would like to get product information from the product identifier in this case; why should i download it? ( $item is a loop of all products of order)

And here I do not need to do any load:

 $customer = $payment->getOrder()->getCustomer(); 

I apologize for my stupid question: what does the download do compared to my second example? Thank you very much and have a nice day,

Anselm

+4
source share
2 answers

Behind the scenes, the effective $payment->getOrder() method (after checking if it is already loaded):

 return Mage::getModel('sales/order')->load($this->getOrderId()); // $this in this context is $payment 

Thus, data loading is still necessary to retrieve the corresponding data from the database, the getOrder() method is just a convenience. The load() method itself returns an instance of the class, so you can assign it to $product in the first example. The getOrder() and getCustomer() methods are not returned, they return another object, therefore $payment not assigned to $customer in the second example.

The Mage::getModel() method is only responsible for determining the correct class and creating an empty instance of it. Instead of loading, you could instead set the data by calling setData() , passing in an array with the key. All setters return their object, as load() does.

+4
source
 $customer = $payment->getOrder()->getCustomer(); 

This means that the client identifier is already present in the session, so you do not need to explicitly tell magento to load the client.

In the case of a product, you must indicate magento the identifier of the product you want to receive.

+1
source

Source: https://habr.com/ru/post/1340004/


All Articles