please answer with simple example of customer detail in which we can access customer detail save() data Delete() getbyid() update() requirements. all functions in controller using searchcriteriabuilder and repo

  • i wanted to implement all function in controller using repo and searchcrieriabuilder. now its ok to not answer i done it by my seniors help.....thanks all for reply :) – Asad Ullah Nov 30 '18 at 21:42
  • Gyz i have answerd it under please review that. thanks – Asad Ullah Nov 30 '18 at 22:12

You should really check out the documentation on this

Most of the modules you see in base Magento are implementations of Service Contracts. Things such as Products, Customers, Orders and more are implemented as Service Contracts.

The most important part of any Service Contract is the Interfaces. You can find these in the Api folder of most modules. These Interfaces provide a way for other modules to interact with this module without knowing the inner details of how everything is implemented.

For example, in the Magento/Customer/Api/Data/CustomerInterface.php file is defined the CustomerInterface. This interface makes certain guarantees about what a Customer class is supposed to be able to do. Every Customer should have a getId() method and a getCreatedAt() method, and many more that are listed in this interface.

Your module, whether it's a chat window or a blog post or a FAQ or an order tracker, doesn't need to know what classes actually implement the CustomerInterface or the technical details of how a billing address is parsed and stored in the database (if it is stored in the database, which some things are not.)

All your module needs to know is that if you have an instance of CustomerInterface, you can do things to it like getId() and getCreatedAt() and many other things. This is the magic of service contracts.

Not only are there data interfaces for the actual data being described (like Customer), there are also service interfaces that provide valuable functions. One very useful type of service interface is the Repository. For example Magento/Customer/Api/CustomerRepositoryInterface.php

Magento Repositories are expected to include the 5 methods:

  1. save(DataInterface)
  2. get(id)
  3. getList(SearchCriteria)
  4. delete(DataInterface)
  5. deleteById(id)

These Interfaces, whether service interfaces or data interfaces are implemented in the Model directory. For example Magento/Customer/Model/CustomerRepository.php implements the CustomerRepositoryInterface and Magento/Customer/Model/Customer.php implements the CustomerInterface.

The di.xml file specifies "preferences" telling the ObjectManager that when somebody asks for CustomerRepositoryInterface, give them the CustomerRepository Model, and when someone asks for CustomerInterface, give them the Customer Model.

Oftentimes, these Models are implemented under the hood by ResourceModels, which connect the Model and all of its business logic to the underlying MySQL database.

The most typical uses of Service Contracts are:

  • Looking up customers, orders, products, etc. using a RepositoryInterface

  • Getting a customer/order/product from a RepositoryInterface, changing it, and then saving it using the same RepositoryInterface.

To get access to a RepositoryInterface, you should use dependency injection by including it in the constructor of your class. For example:

class MyClass {

    protected $customerRepository;

    public function __construct
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
    ) {
         $this->customerRepository = $customerRepository;

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.