Skip to main content
106 votes
Accepted

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

If there is a repository and it does what you need well, always prefer the repository. Repositories are part of the Service Contracts (they are implementations of interfaces in Api), this means they ...
Fabian Schmengler's user avatar
30 votes

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

Good question. Even if both Repositories and Factories let us access an Entity I think we should focus on their responsibility. From Magento documentation: "Factories are service classes that ...
Alessandro Ronchi's user avatar
26 votes
Accepted

Is there ever a reason to prefer $model->load() over service contracts?

The reason to use ProductRepository's get/getById instead of a ProductFatory's load() method, is because the former is of a higher level than the latter. A ProductRepository - just like a ...
nevvermind's user avatar
  • 1,736
16 votes
Accepted

Are collections history in Magento 2?

Collections are not deprecated now. While some modules already expose Service Contract APIs, others still expose only Model/Collection APIs. The plan is: Reflect current state with better @api ...
Anton Kril's user avatar
  • 4,496
15 votes

How should I save new or updated entities of models?

Let us first see, what happens if you use the save() method directly on a product model like /** * @var Magento\Catalog\Model\Product $product */ $product->save(); The model class itself is ...
Marjan's user avatar
  • 1,139
14 votes

Is there ever a reason to prefer $model->load() over service contracts?

To me, there is no reason to use the load method over the getById / get method. I don't say that I'm right but here's how I see things. Ok so here's the getById method (get method is similar but ...
Raphael at Digital Pianism's user avatar
10 votes
Accepted

Magento2: Difference between Factory and Repository

Different between factory and repository Factory definition: Factories are service classes that instantiate non-injectable classes, that is, models that represent a database entity. They create a ...
Amit Bera's user avatar
  • 77.6k
7 votes
Accepted

Customer Repository Interface, Limit Results

Figured it out. http://devdocs.magento.com/guides/v2.2/extension-dev-guide/searching-with-repositories.html $searchCriteria = $this->searchCriteria ->setFilterGroups([$...
Ethan Yehuda's user avatar
7 votes

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

I would say the way forward it's to start using repositories as they allow code separation between data reading/writing and business logic. There is a very detailed article written by Alan Storm ...
Marina Gociu's user avatar
  • 1,261
6 votes
Accepted

Can the NPM fiasco happen to Magento 2?

It's not that bad. composer show -it shows a huge tree, but if you leave out the repositories hosted by Magento itself and the PHP extension requirements, the external requirements are: Composer ...
Fabian Schmengler's user avatar
6 votes
Accepted

How do you get the new product ID after programmatically creating the product?

Okay, here's the situation: Because of how the product save works, $this->_productRepository->save($newProduct); does not reflect changes back onto your $newProduct. It's actually creating a new ...
Ryan Hoerr's user avatar
  • 12.3k
5 votes
Accepted

Magento 2: save product description for adminhtml programmatically

Set the current store by name: $this->_manStore->setCurrentStore("admin");
Jacques's user avatar
  • 1,022
5 votes
Accepted

SearchCriteriaBuilder->addFilters() using multiple filters

In the constructor, notice that I appended the word "Factory" to the classname of the collection. This is a non-existent factory class that Magento will actually create for you. In the function, where ...
Shawn Abramson's user avatar
5 votes

Magento 2: When should we use a ProductRepository, ProductFactory, Product model?

First, check below links for understanding Repository pattern https://inchoo.net/magento-2/repository-pattern-in-magento-2/ https://devdocs.magento.com/guides/v2.3/extension-dev-guide/service-...
Amit Bera's user avatar
  • 77.6k
4 votes
Accepted

In Repository, how to replace deprecated load() function?

You can use ResourceModel's load() instead, which extends Magento\Framework\EntityManager\EntityManager's load() method. We could see a example in: Magento\Cms\Model\BlockRepository public function ...
Yogesh Agarwal's user avatar
4 votes
Accepted

Resource Model vs Repository in Magento 2

Resource models are the way models were handled in Magento 1 and are still available in Magento 2. Repositories are the way we are supposed to handle model storage and retrieval in Magento 2, but ...
Petar Dzhambazov's user avatar
4 votes
Accepted

What PHP class executes the SQL to select models and collections from the database?

In most circumstances, entity models inherit from AbstractModel, resource models, which serves as an adapter between an entity model and ORM, inherit from AbstractDb, and collections inherit from ...
nburr's user avatar
  • 191
3 votes

Can the NPM fiasco happen to Magento 2?

it's definetely possible. If they are not already doing it, Magento could mirror all needed Packages on repo.magento.com. Although not sure what's the legal situation here
David Verholen's user avatar
3 votes

Magento 2 - Custom Repository does not create new entity

I found the solution through this post how-set-resource-model-for-not-primary-ai :). In nutshell, because my primary key is not auto-increment, the resource model fails the save method for new ...
Mirko Rapisarda's user avatar
3 votes

Delete all entries in a custom table programatically

If you need to delete all the data row from the database table then you can be used the below code to truncate the table using the model. $connection = $model->getCollection()->getConnection(); $...
Rasik Miyani's user avatar
2 votes

Is there ever a reason to prefer $model->load() over service contracts?

Please understand the difference between repositories and collections. In your example, if using repositories, you will get an array of Magento\Catalog\Api\Data\ProductInterface that is different ...
Phoenix128_RiccardoT's user avatar
2 votes

Magento 2: Product Repositories, Filter Groups, and `AND`

In Magento 2 all the filters in same FilterGroup will be added using OR operator. But all the FilterGroup will be added using AND operator. So you will need to add multiple FilterGroups as below: $...
Kamal Singh's user avatar
2 votes

How should I save new or updated entities of models?

It is encouraged to use data interfaces (e.g. \Magento\Catalog\Api\Data\ProductInterface) instead of the model directly and use the repositories to load and save models. See Magento developer ...
ochnygosch's user avatar
2 votes

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

Hope this answer might help other extension developers as well. We have to save model using Repository only. Factory Model in Magento 2 hold very limited data. On the Other hand, Repository Model ...
Sachin's user avatar
  • 111
2 votes

Repo with sources for Magento 1

Unlike Magento 2, there is no official Magento 1 repo where you can do pull requests. On a side note, Inchoo released a free module that makes Magento 1 compatible with PHP 7: https://github.com/...
Raphael at Digital Pianism's user avatar
2 votes
Accepted

The difference in use Repository or RepositoryInterface to load model

You can see in the core. The class \Magento\Catalog\Model\ProductRepository implements interface \Magento\Catalog\Api\ProductRepositoryInterface. So basically, you can use the function get($sku) of ...
Nero Phung's user avatar
  • 1,318
2 votes

magento sampledata:remove not working

Removing Magento sample data, unfortunately, is not as easy as to install it. Running the command php bin/magento sampledata:remove Doesn't really remove all products from your Admin Inventory, ...
Iveta Allogenes's user avatar
2 votes

Magento 2.3.0 How to add store filter in searchCriteriaBuilder

If you want to pass multiple storeId to Filter You can use in for multi-store select. So your code looks like below : $searchCriteria = $this->searchCriteriaBuilder ->...
Himanshu's user avatar
  • 1,761
2 votes

Magento 2: When should we use a ProductRepository, ProductFactory, Product model?

When should USE ProductRepository Product repository is generally used to call data collection of product which is basically call ResourceModel at the end. When you call Product Repository it will ...
Himanshu's user avatar
  • 1,761
2 votes

Invalid credentials for repo.magento.com

At Magento root add auth.json and remove other auth.json or auth.json.sample file. { "http-basic": { "repo.magento.com": { "username": "...
Msquare's user avatar
  • 9,133

Only top scored, non community-wiki answers of a minimum length are eligible