2

While going through the Magento code I have been seeing a lot of instances of:

$objectName->load($foo)

or

$objectName->get($foo)

and I was wondering, what is the difference between the 'load' and 'get' methods?

5

If you have to talk about the load and get at Magento2.

Then let me clear that

load() is the function of Model class.

get()is a function of Repository class of service contract.

Both are used for fetch the data of particular u entity like Product, Cms Page etc using Primary key.

But load function is deprecated at Magento 2, You have to use the repository class for getting particular table/entity data.

See detail When Should We Use a Repository and Factory in Magento 2?

| improve this answer | |
3

In short, In Magento2 the best way is to use the service contract get() when it's possible, if it's not available you can use ResourceModel to save the entities, however the load() like save(), delete() also, are a deprecated methods.

You can see :

Why use Service contract

Deprecated save and load methods in Abstract Model

| improve this answer | |
0

Get ( ) and Load ( ) are Hibernate Sessions. They provide with different methods to fetch data from the database. Get ( ) returns the object by fetching it from database Whereas Load ( ) just returns the reference of an object that might not actually exist. It lodes the data from the database or catch only when you access other properties of the object. Get ( ) helps in retrieving the data that doesn't exist.

The main difference between Get ( ) vs load methods is that get ( ) Lodes the data as soon as it’s called, however, Lodes ( ) supports lazy loading and returns to proxy objects and lodes data as per the requirement.

| improve this answer | |

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.