Cheerymagicsoftware official page


Programming is a cheery magic!


Magento 2 Repositories and Factories

Author Poliakov Vladimir.
As a continuation of the previous post, let’s talk about repositories and factories. Many entities in Magento 2 such as the product, category customer, address, quote, order, and others for “CRUD” operation, better use of the repository and factory. If the version of Magento 2.1 to be able to work directly with the methods AbstractModel load, save, delete:


        <?php
    $address_id = 5;
    $customer_id = 5;
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance;
    $address = $objectManager->create('\Magento\Customer\Model\Address');// Bad practice! Load classes over constuctor! It's only for example.

    $address->load($address_id);

    $address->setCustomerId($customer_id)
        ->setCountryId('US')
        ->setFirstname($this->getRequest()->getPostValue('firstname'))
        ->setLastname($this->getRequest()->getPostValue('lastname'))
        ->setTelephone($this->getRequest()->getPostValue('telephone'));
    //You can't get/set custom attributes for EAV, because AbstractModel Class don't have repo.

    $address->save();

That they are now is deprecated, so that if you use these methods, the further upgrade version of Magento at your own risk:

http://devdocs.magento.com/guides/v2.1/extension-dev-guide/persistent-entities.html#models


        <?php
    $address_id = 5;
    $customer_id = 5;
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance;
    $addressRepo = $objectManager->create('\Magento\Customer\Api\AddressRepositoryInterface');// Bad practice! Load classes over constuctor! It's only for example.

    $address = $addressRepo->getById($address_id);
    $address->setCustomerId($customer_id)
        ->setCountryId('US')
        ->setFirstname($this->getRequest()->getPostValue('firstname'))
        ->setLastname($this->getRequest()->getPostValue('lastname'))
        ->setTelephone($this->getRequest()->getPostValue('telephone'));
    $address->setCustomAttribute('delivery_instructions', 'My custom Delivery Intruction'); //You can get/set custom attributes.
    $addressRepo->save($address);

And if you’re going to add a new address through the repository, then you need create a new instance through the factory:


        <?php
    //...
    $address = $objectManager->create('\Magento\Customer\Api\Data\AddressInterfaceFactory');
    $address->setCustomerId($customer_id)
        ->setCountryId('US')
        ->setFirstname($this->getRequest()->getPostValue('firstname'))
        ->setLastname($this->getRequest()->getPostValue('lastname'))
        ->setTelephone($this->getRequest()->getPostValue('telephone'));
    //Add needs data for new address entity
    //...
    $address->setCustomAttribute('delivery_instructions', 'My custom Delivery Intruction'); //of course custom attribute.
    $addressRepo->save($address);

The repository has methods to remove at id and other methods. Implementation of methods repository’s interfaces are typically located in the resource model entities. For example, the implementation of \Magento\Customer\Api\AddressRepositoryInterface is in Magento\Customer\Model\ResourceModel\AddressRepository.

Protected by Copyscape