How to Use Data-related Classes, Repositories and Data API in Magento 2

Jul 24, 2018454Andrey Matveev
How to Use Data-related Classes, Repositories and Data API in Magento 2

This is Part 1 of the series of articles dedicated to using data-related classes, repositories and data API in Magento 2. Today we will touch upon two questions: obtaining an object or set of objects from the database using a repository and configuring and creating a SearchCriteria instance using the builder.

Obtaining an object or set of objects from the database using a repository

To work with collections in Magento 2, you can use a repository that implements the same repository pattern. The Repository pattern allows working with collections as entities, regardless of their storage location (storage or caching are implementation details). The pattern itself is located between the Domain and the Service Level.

In Magento 2, there are usually 5 basic functions of the repository: save, getById, getList, delete, deleteById. But each Repository implementation in Magento has its own interface and these functions are not always implemented in it. For example, in Magento\Quote\Api\GuestCartTotalRepositoryInterface, only the get($cartId) method is implemented. Therefore, it is better to pay attention to the implementation of a particular Repository class.

Let’s take a look at the Repository using the example of \Magento\Catalog\Api\ProductRepositoryInterface and its implementation \Magento\Catalog\Model\ProductRepository.

Here all 5 basic functions for working with Repository are implemented. Meanwhile, the main class when working with a product is \Magento\Catalog\Api\Data\ProductInterface, which allows you to save all products inherited from this class, regardless of their type.

Let’s look at the implementation of the getById method:

Here we implement the work with ProductRepository, extracting the object with id 1 and returning its SKU. Similarly, the deleteById method will be implemented. In the constructor, as you can see, we create \Magento\Catalog\Api\ProductRepositoryInterface, and not \Magento\Catalog\Model\ProductRepository itself. These dependencies are described in the di.xml file. For example, for the current class, the dependency is described in vendor/magento/module-catalog/etc/di.xml and looks like this:

Here we have changed the SKU of the product and saved it using the save repository method. The delete method works similarly (but with the opposite result).

Configuring and creating a SearchCriteria instance using the builder

The getList method, in this context, is implemented using SearchCriteriaInterface. This interface implements the conditions for queries (for example, in MySQL this is where in the query). All conditions are divided into Filter and FilterGroup.

Filter and FilterGroup

We implement the abovementioned conditions using SearchCriteria:

This example returns an array of objects inherited from \Magento\Catalog\Api\Data\ProductInterface.

This is the end of part 1 of the series dedicated to data-related classes, repositories and data API in Magento 2. The other parts are coming up soon, stay tuned.



Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *