Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now

Questions tagged [repository]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
75
votes
5answers
21k views

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

I have gone through a couple of tutorials in Magento 2, and this confuses me a little. I can see there are basically two ways by which we can read/write business entities: Retrieve Data Using A ...
24
votes
2answers
19k views

Filtering a Magento 2 Object Repository

In Magento 2, can you use a product repository to filter by product attributes? In Magento 2, you can use a search criteria object \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria, ...
24
votes
1answer
758 views

Are collections history in Magento 2?

I know that a lot of code that is currently in Magento 2 (2.1.2) is more or less ported from Magento 1 and that a lot of code will be substituted by an equivalent in the future. In this aspect, I'm ...
23
votes
3answers
3k views

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

I understand that the preferred way to work in between modules in Magento 2 is using the service contracts. So if I want to load a product, I use the product repository: $product = $...
12
votes
2answers
9k views

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

I'm trying to use a product repository to fetch a list of products. I want to fetch based on two filters, combined with an AND criteria, but things don't seem to be working. Do I not understand how ...
9
votes
2answers
5k views

How should I save new or updated entities of models?

In Magento 2 we have repositories classes. Classic method save() used heavily in Magento 1.9 are deprecated, if I am correct, from 2.04 or 2.05. I was using factories to create new object and after ...
6
votes
2answers
943 views

Can the NPM fiasco happen to Magento 2?

As some of you may know, a huge fiasco happened a month ago with NPM As of now, Magento 2 composer.json file includes a lot of external resources (details on the Magento 2 GitHub) What if one of ...
5
votes
0answers
67 views

StoreRepository get($code) which does not exists

Till today all the code was okay, but after code review, I got that we use the method load() on the store itself so I replaced it with storeRepository->get($code) Code what I'm talking about ...
5
votes
0answers
468 views

_origData is null for some models in Magento 2 before or after save

Let's get a customer save process from the admin panel as an example. We have an event observer for model_save_before and trying to get a set of values for the model that have been changed upon the ...
4
votes
2answers
304 views

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

When should we use a ProductRepository, ProductFactory, Product model? vendor/magento/module-catalog/Model/Product.php vendor/magento/module-catalog/Model/ProductRepository.php generated/code/...
4
votes
1answer
2k views

Magento2: Difference between Factory and Repository

What is the difference between these two objects in magento2? (Factory and Repository) I have seen that they have almost the same methods. What proper use does one have?
4
votes
1answer
845 views

Resource Model vs Repository in Magento 2

I am quite confusing on the concept of Resource Model and Repository in Magento 2. Are they both responsible for managing the persistent storage, like MYSQL? Let says I would like to get specific ...
4
votes
0answers
82 views

How to handle multiple tables with magento 2 repositories?

As we all know, magento 2 suggests using repositories as an additional layer between resource models/collections and the rest of application. It's all pretty straightforward and simple when you use it ...
4
votes
1answer
2k views

'Option values are not specified' when saving configurable

I have a product import routine which calls $this->productRepository->save($product); but it's failing with the following error: [Magento\Framework\Exception\InputException] Option values are ...
3
votes
1answer
3k views

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

In Magento 2, it's not possible to add a store_id filter to a product collection directly. Instead, you need to use the addStoreFilter method, which adds a special "product limitation filter". It ...
3
votes
2answers
96 views

Magento 2.3.0 How to add store filter in searchCriteriaBuilder

In my custom module, I am trying to add store filter in orderRepository using searchCriteriaBuilder but it is throwing an error like below. 1 exception(s): Exception #0 (Exception): Warning: PDO::...
3
votes
2answers
168 views

The difference in use Repository or RepositoryInterface to load model

I found two ways to load model on net by service contracts in Magento 2. One is use the Repository, another is using the RepositoryInterface directly. Both work well. What is the difference between ...
3
votes
2answers
4k views

How to join a table with a Magento 2 repository

I am trying to extend inventory stock items by joining my own table with added information. When the product page is loaded it gets the stock item via StockItemRepository::getList($criteria). So I ...
3
votes
0answers
256 views

2.2 Pipeline deployment

I was reading on the new Pipeline deployment here but I don't understand how this is supposed to work in a repository. In developer mode static content is dynamic, but if the build system commits ...
2
votes
1answer
2k views

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

I have a configurable product and am trying to programmatically create a new simple product and then associate it with my configurable product. This is what I have so far... $newProduct = $this->...
2
votes
1answer
1k views

Customer Repository Interface, Limit Results

I'm trying to grab the oldest customer, using the customer repository from the database. Here's my code so far: // if custom is active $isActivefilter = $this->filter ->...
2
votes
1answer
790 views

Magento 2: save product description for adminhtml programmatically

I cannot see description and short description attributes of the product in adminhtml after saving: /** @var \Magento\Store\Model\StoreManagerInterface */ protected $_manStore; /** @var \Magento\...
2
votes
1answer
60 views

Repo with sources for Magento 1

Where is the source code repo for Magento 1? I want to post PHP7 incompatibility fix for the latest version (1.9.2.4).
2
votes
1answer
424 views

Update Product programmatically - Invalid method Magento\Eav\Model\Entity\Attribute::isScopeGlobal

I have a quite simple piece of code (reduced it to that): try { $newProduct = $this->productRepository->get($productData['sku']); echo 'Loaded.' . PHP_EOL; $savedProduct = $this->...
2
votes
3answers
1k views

“URL key for specified store already exists” when updating an existing product?

I am getting the above error on a limited number of products, when loading and saving them with this code: $product = $productRepo->get($_sku); $product->setStockData(['qty'=>$new_qty,'...
2
votes
0answers
18 views

taxClassId are not saved with the rest of the product

I'm trying to update some attributes of my product by a cron to avoid common mistakes. The code causing me trouble is executed in a loop: foreach ($storesIds as $storeId) { $this->...
2
votes
0answers
78 views

Update select attribute in custom_attributes via catalogProductRepositoryV1 API PUT request

I'm trying to update a product with the catalogProductRepositoryV1. I want to change a custom select attribute for the given product. When sending the following request: { "product": { "...
2
votes
1answer
977 views

Warning: Illegal offset type in vendor/magento/module-catalog/Model/CategoryRepository.php on line 133

I am getting Warning: Illegal offset type in vendor/magento/module-catalog/Model/CategoryRepository.php on line 133 on product details page in Magento 2.1.8 Which is caused by vendor/...
2
votes
0answers
406 views

Magento2 Compile command ends at Repositories code generation 1/7

Magento v2.1.7 CE PHP 7.0 Permissions double checked When I run compile command through cli, it ends at step 2 as seen in the image below. I found that it might be due to a wrong auth.json in ...
2
votes
0answers
1k views

Add image to product programatically not using addImageToMediaGallery

Method addImageToMediaGallery() seems to me buggy, when using productRepository to save product, what encourage us to use deprecate method save() directly from product model (what causes other bugs), ...
1
vote
1answer
30 views

Magento 2 - Custom Repository does not create new entity

I create a custom entity and repository. All work well, but the POST API route does not create new entities. But if I call the PUT API route to change a property, it works well. Why my custom ...
1
vote
1answer
35 views

Add to cart not working in custom template magento2

I am getting add to cart url by product object like below. Block file. use Magento\Catalog\Api\ProductRepositoryInterface; use \Magento\Checkout\Helper\Cart as CartHelper; class CustomAdd ...
1
vote
1answer
66 views

How do I connect a Magento database and a cloned repository on my local machine?

I am working with a magento 2 build via a repository clone. I have the dumped sql database from the repository build. I imported the sql into my localhost DB, how do I connect them so that a local ...
1
vote
1answer
31 views

Magento 2 - What should repository methods return?

This question has been on my mind for some time. I'm currently writing a repository with the usual methods: save, getById, getList, delete and deleteById. What is the correct return for each method? ...
1
vote
0answers
116 views

Magento 2.3 Composer stuck while downloading https://repo.magento.com/packages.json

I have a problem since about 24 hours I guess. When I try to download Magento 2 with composer I stuck after logging with my user-credentials. The credentials are ok, it pass and go through the next ...
1
vote
0answers
42 views

API issue returning object

We have this API where we save different fields, but the object $lines we have created doesn't save properly. The code below are the getters and setters for the object $lines. Save method API: ...
1
vote
1answer
70 views

custom extensions - are hyphens allowed in the Extension name?

This is a follow up on the Magento 2 Extension naming convention question here. I've learned thus far that you need a Vendor Name an Extension (or Module) name a combination of both like ...
1
vote
0answers
22 views

Magento 2 REST API create for post operation

I am a newbie in Magento. I am trying to create a Magento Rest API for post operation.I go through many online resources but none of it clarify my doubt. I am trying to create an API for an operation ...
1
vote
0answers
27 views

Magento 2 BlockRepository.php can't understand code

I'm newbie at Magento 2, and trying to study and research code to use all business logic for my future work. I found method getList at BlockRepository.php at https://github.com/magento/magento2/blob/...
1
vote
1answer
27 views

How To Avoid Using the Save method with custom objects in Magento 2

The save method of AbstractModel is marked to be deprecated, for this reason I try to avoid using it. Normally I do this by using a repository. That's fine for things like orders which Magento ...
1
vote
1answer
41 views

SetId on category results on exception

I would like to set my ID on the category Category::setId but this results in the following exception: "No such entity with id = x". My code: $catFactory = $objectManager->get(CategoryFactory::...
1
vote
0answers
601 views

How to manage custom attributes in group customer

i'm new in the magento world (and php community). I want to add some attributes to custom group in magento 2.2 I've created a new attribute by setup (his name is : 'exchange_id') : use Magento\...
1
vote
0answers
368 views

How to load a model with relationships via repository in Magento 2?

Background story: I was trying to load a model with model_store relationship using this snippet. I failed to do that with my own model. Model is being loaded without store data. Some debuging later I ...
1
vote
0answers
305 views

Product repository - get methods' store id argument = null

If I do not provide $storeId argument for methods get() or getById what store id would be used? Default? Or current? If I DO use this argument we can see that it will be set and data specific for ...
1
vote
1answer
307 views

Magento 2: links between entities: best practices

I have a question regarding link between entities. Consider that a car is linked to a brand. The car entity will have a brand_Id attribute. Coming from the "Symfony universe", using the Doctrine ORM, ...
1
vote
2answers
2k views

How can I check if a product exists in Magento 2 without throwing \Magento\Framework\Exception\NoSuchEntityException?

I'm attempting to scan an incoming list of products and add any new ones to the catalog. Therefore, I must run a check on each product to see if it already exists. I'm using a ...
0
votes
1answer
1k views

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

I know we should use service contracts(Repository) over $model->load(), but in Repository itself, how to replace the deprecated load() function? For example, in Magento\Cms\Model\PageRepository, ...
0
votes
2answers
25 views

Create QuestionManagement class

I have made Question repository class: namespace Vendor\Module\Model; use Magento\Framework\Api\DataObjectHelper; use Magento\Framework\Api\SearchCriteriaInterface; use Magento\Framework\Api\Search\...
0
votes
1answer
2k views

SearchCriteriaBuilder->addFilters() using multiple filters

As per title. This what I've done: $filterCode = new Filter(); $filterCode->setField('SpecialCode') ->setValue('') ->setConditionType('null'); $filterItemId = ...
0
votes
1answer
344 views

magento sampledata:remove not working

I want to remove sample data or reinstall magento 2.2.1 without sample data i tried below command. php bin/magento sampledata:remove command is not working and throwing below error [Composer\...