Table of content

Magento 2 Certified Professional Developer Guide

Section 4: Working with Databases in Magento

4.1 Demonstrate ability to use data-related classes

Describe repositories and data API classes. How do you obtain an object or set of objects from the database using a repository?

Repository can be applied for working with collections in Magento 2. It realizes the Repository pattern that allows to work with collections and entities regardless of their storage place (storing or caching are the implementation of details). The pattern itself is located between Domain and Application Service Layer.

In Magento 2, five basic repository functions are realized. They are save, getById, getList, delete, deleteById. Yet, each Repository realization in Magento has a custom interface and the functions are not always implemented in it. For example, in Magento\Quote\Api\GuestCartTotalRepositoryInterface, only get($cartId) method is realized. Therefore, it is recommended to pay attention to a certain Repository class implementation.

Let us examine the Repository case using the \Magento\Catalog\Api\ProductRepositoryInterface example and its realization

\Magento\Catalog\Model\ProductRepository.

Here, all necessary five functions for working with Repository are realized. In this case, the main class for working with product is \Magento\Catalog\Api\Data\ProductInterface, allowing to save all the products inherited from this class, regardless of their type.

Let us consider the example of getByld method realization:

In this case, we realize working with ProductRepository by extracting the object from id 1 and returning it to SKU. deleteByld method will be realized similarly. In this constructor, we create \Magento\Catalog\Api\ProductRepositoryInterface, not \Magento\Catalog\Model\ProductRepository itself. The dependencies are described in di.xml file. For example, the dependency for the current class is described in vendor/magento/module-catalog/etc/di.xml and looks the following way:

Let us also consider save method realization:

We modifies SKU of the project and saved it using save repository method. Delete method deletes the product.

How do you configure and create a SearchCriteria instance using the builder?

In this context, getList method is realized with SearchCriteriaInterface. The interface realizes conditions for requests (for example, it is where in MySQL query). All conditions fall into two categories: Filter and FilterGroup.

Magento 2 Certified Professional Developer Guide Screenshot 22

We implement the conditions mentioned above using SearchCriteria:

This code returns the objects array, inherited from \Magento\Catalog\Api\Data\ProductInterface.

How do you use Data/Api classes?

Api in Magento 2 is commonly used to describe interfaces, further used in Model, Helper, Data and other Magento classes. Also, API interfaces can be applied for WebAPI requests (when they are described in webapi.xml). Api directory is located in modules roots, similarly to etc, Model and other directories.

Unlike Api, Api/Data directory contains interfaces for the data, for example, store data or customer data.

An excellent example for explaining the difference between Api and Api/Data is the implementation of \Magento\Catalog\Api\ProductRepositoryInterface and \Magento\Catalog\Api\Data\ProductInterface.

ProductRepository implements a get method that loads a Product object (using ProductFactory) that implements ProductInterface for working with data.

Describe how to create and register new entities

In Magento 2, entities are unique objects that contain a number of various attributes and/or parameters. Products, orders, users, etc. are all examples of entities.

To create a new entity type, use \Magento\Eav\Setup\EavSetup class. Entity creation and registration is realized via installEntities($entities = null) method, specifying the entity settings as a parameter. For example:

How do you add a new table to the database?

To add a new table, create a Setup script that realizes Magento\Framework\Setup\InstallSchemaInterface or Magento\Framework\Setup\UpgradeSchemaInterface interface, or realize install or upgrade method in it correspondingly. For example:

This part of the code creates a belvg_test table with entity_id and first_attribute fields.

Describe the entity load and save process

To load and store entity in Magento\Eav\Model\Entity\AbstractEntity model, load and save methods are realized; they carry the logic of saving and loading from the database.

Describe how to extend existing entities. What mechanisms are available to extend existing classes, for example by adding a new attribute, a new field in the database, or a new related entity?

To extend the existing classes, you can create additional EAV attributes and new fields, as well as create the connected classes that realize their interface.

To create additional fields in the database, apply Setup scripts. Let us examine how to create a field in the database using InstallSchema class as an example:

We extended the existing class with an additional ‘custom_field’ field in ‘custom_table’ table.

A new attribute can be created using \Magento\Eav\Setup\EavSetup class. Example:

Extension Attributes were introduced in Magento 2; they are responsible for adding the additional data into the created object. Extension attribute should use ExtensibleDataInterface interface. Also, you are advised to realize getExtensionAttributes and setExtensionAttributes methods in your code.

Code for extension attribute is generated during the compilation process, using \Magento\Framework\Code\Generator that applies etc/extension_attributes.xml file from the module directory. Example of extension_attributes.xml :

Unlike the common Magento attributes, extension attribute is not automatically loaded from and stored into the database, which means you need to realize the loading and saving manually. For this purpose, plugins are the best choice; they are declared in di.xml file. Example:

In the plugin, we can realize afterGet and afterSave methods that will contain the loading and saving extension attribute logic.

Describe how to filter, sort, and specify the selected values for collections and repositories. How do you select a subset of records from the database?

Use addFieldToSelect and addAttributeToSelect methods to specify in the collection the fields for selection.

For example:

$productCollection->addFieldToSelect("custom_field");

To apply filters to collections, use

addAttributeToFilter($field, $condition) and addFieldToFilter($field, $condition) methods.

Conditions can be the following:

“eq” => equalValue

“neq” => notEqualValue

“like” => likeValue

“nlike” => notLikeValue

“is” => isValue

“in” => inValues

“nin” => notInValues

“notnull” => valueIsNotNull

“null” => valueIsNull

“moreq” => moreOrEqualValue

“gt” => greaterValue

“lt” => lessValue

“gteq” => greaterOrEqualValue

“lteq” => lessOrEqualValue

“finset” => valueInSet

“from” => fromValue, “to” => toValue

Example:

$productCollection->addFieldToFilter('entity_id', array('in' => [1,2,3])

setOrder method is used for sorting and processes both filter and direction fields. For instance:

$productCollection>setOrder('position','ASC');

searchCriteria is used for applying filters in repositories.

Describe the database abstraction layer for Magento. What type of exceptions does the database layer throw? What additional functionality does Magento provide over Zend_Adapter?

Database abstraction layer realized in Magento 2 in the capacity of \Magento\Framework\Model\ResourceModel\Db\AbstractDb class, realizing such core methods, as save, delete, load.

Also, the following additional methods are realized atop Zend_Adapter:

addUniqueField, unserializeFields, serializeFields, hasDataChanged, prepareDataForUpdate, isObjectNotNew, saveNewObject, afterSave, beforeSave, isModified, afterLoad, beforeDelete, afterDelete и прочие.

Database layer can have put our exceptions depending on its realization. For example, PDO/Mysql can put out the following exceptions:
Zend_Db_Adapter_Exception

Zend_Db_Statement_Exception

Zend_Db_Exception

Zend_Db_Statement_Pdo

PDOException

LocalizedException

InvalidArgumentException

Exception

DuplicateException

4.2 Demonstrate ability to use install and upgrade scripts

Describe the install/upgrade workflow. Where are setup scripts located, and how are they executed?

Magento 2 applies schema/data migrations to provide data persistence and database updatability. The migrations contain instructions for:

  1. The necessary tables creation and their completion at the initial installation
  2. Database scheme and its information conversion for each available application version.

Magento 2 setup scripts are located in <module_dir>/Setup folder.

InstallSchema and InstallData classes are responsible for installing module the first time, while UpgradeSchema and UpgradeData scripts are used when upgrading module’s version.

Running setup scripts

Use CLI command to run migration scripts:

$ php bin/magento setup:upgrade

If Magento detects a new module, then it will instantiate objects from the Vendor\Module\Setup\InstallSchema and Vendor\Module\Setup\InstallData classes.

In case the module version has changed, then Vendor\Module\Setup\UpgradeSchema and Vendor\Module\Setup\UpgradeData will be instantiated. Afterward, the corresponding upgrade methods will be executed.

Versioning

Unlike Magento 1, Magento 2 does not contain the inbuilt migration versioning tools, meaning that a developer must check the current module version manually.

Which types of functionality correspond to each type of setup script?

InstallSchema class

This setup script is used for modifying database structure at the module’s first installation.

InstallData class

This setup script is applied for adding and modifying the data at the module’s first installation.

UpgradeSchema class

The setup script is applied for modifying database structure at the module update.

UpgradeData class

This setup script is applied for adding and modifying the data in the event of module upgrade.

Recurring scripts

Recurring scripts are run each time setup:upgrade command is launched and depend on the module’s version.

<module_dir>/Setup/Recurring.php

Igor-Dragun

Partner with Us

Send us a message to grow your business. Get a free quote now. Speak to us +16503532301

Send my message