99 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 ...
user avatar
39 votes
Accepted

Filtering a Magento 2 Object Repository

Check out the following sample class. To filter by SKU, try this: $productFilterDemo->getProducts('sku', 'product_sku_value', 'eq'); To get products created after specific date, this: $...
user avatar
27 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 ...
user avatar
25 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 ...
user avatar
  • 1,676
18 votes
Accepted

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

Actual annotation of \Magento\Framework\Api\Search\FilterGroup says (class phpDoc): Groups two or more filters together using a logical OR It means that you need to create two groups with one ...
user avatar
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 ...
user avatar
  • 4,446
16 votes

Filtering a Magento 2 Object Repository

public function __construct( ProductRepositoryInterface $productRepository, SearchCriteriaBuilder $searchCriteriaBuilder, FilterBuilder $filterBuilder, ) { $this->productRepository =...
user avatar
  • 956
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 ...
user avatar
13 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 ...
user avatar
  • 1,079
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 ...
user avatar
  • 75.9k
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 ...
user avatar
  • 1,241
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 ...
user avatar
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 ...
user avatar
5 votes
Accepted

Magento 2: save product description for adminhtml programmatically

Set the current store by name: $this->_manStore->setCurrentStore("admin");
user avatar
  • 1,027
5 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([$...
user avatar
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 ...
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-...
user avatar
  • 75.9k
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 ...
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 ...
user avatar
4 votes
Accepted

Magento 2: Can you Filter By store_id Using a Product Repository

Bug is created to add filtration capability by store ID, see this response. The following plugin can be used as a workaround before the issue is fixed in the core. It allows to avoid hacks in the ...
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 ...
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
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 ...
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 ...
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: $...
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 ...
user avatar
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/...
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 ...
user avatar
  • 101
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 ...
user avatar
  • 1,273
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 ->...
user avatar
  • 1,681

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