Reading list Switch to dark mode

    Create Repositories in Magento 2

    Updated 21 February 2024

    Magento-Code-Snippet-5-2

    Today we focused on how to create repositories for your module.

    What is the repository?

    Repositories are service contracts which are interface classes, and help to hide your business logic from the controller, model, and helper.

    To create your module’s repository, firstly you have to define it in the di.xml file at path:

    app/code/Webkul/Hello/etc/di.xml

    Searching for an experienced
    Magento 2 Company ?
    Find out More
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <preference for="Webkul\Hello\Api\TestRepositoryInterface" type="Webkul\Hello\Model\TestRepositoryModel" />
    </config>

    In this file, we defined our repository file which is an interface class, and model file in which we define our methods declared in the repository class.

    Now, create the repository file at path:

    Webkul\Hello\Api\TestRepositoryInterface.php

    <?php
    
    namespace Webkul\Hello\Api;
    
    interface TestRepositoryInterface
    {
        /**
         * Create or update a data
         */
        public function save(\Webkul\Hello\Api\Data\TestInterface $test);
    
        public function getById($testId);
    
        /**
         * Delete test.
         */
        public function delete(\Webkul\Hello\Api\Data\TestInterface $test);
    
        /**
         * Delete test by ID.
         */
        public function deleteById($testId);
    }

    Here, we declare our methods, like, getById, delete, deleteById, save.

    Now, create a model file in which we define our methods, at path:

    Webkul\Hello\Model\TestRepositoryModel.php

    namespace Webkul\Hello\Model;
    
    class TestRepositoryModel implements \Webkul\Hello\Api\TestRepositoryInterface
    {
    
        /**
         * Save test data.
         */
        public function save(\Webkul\Hello\Api\Data\TestInterface $test)
        {
            //your code
        }
        /**
         * Retrieve test data.
         */
        public function getById($testId)
        {
            //your code
        }
    
        /**
         * Delete test.
         */
        public function delete(\Webkul\Hello\Api\Data\TestInterface $test)
        {
            //your code
        }
    
        /**
         * Delete test by test ID.
         */
        public function deleteById($testId)
        {
            //your code
        }
    }



    Here, we define all the methods that we declared in our repository class.

    Here, \Webkul\Hello\Api\Data\TestInterface in the data interface in which we define our data entities and their respective methods.

    Now, repositories are created for your module.

    How to use repositories?

    Here, is an example of a controller, in which we have a test ID, and by the use of repositories, we use the deleteById() function.

    namespace Webkul\Hello\Controller\test;
    
    use Webkul\Hello\Api\TestRepositoryInterface;
    
    class Delete extends Action
    {
    
        protected $_testReporitory;
    
        public function __construct(
            Context $context,
            TestRepositoryInterface $testReporitory
        ) {
            $this->_testReporitory = $testReporitory;
            parent::__construct(
                $context
            );
        }
    
        public function execute()
        {
            try {
                $testId = 10;//any id
                $this->_testReporitory->deleteById($testId);
            } catch (\Exception $e) {
                $this->messageManager->addException($e, $e->getMessage());
            }
        }
    }

    I hope this blog will help you with creating repositories in Magento 2. You may also check our wide range of best Magento 2 Extensions.

    Please reach out to our team via a support ticket if you have any queries.

    Try this and if you have any queries then just comment below 🙂

    Hope, it will help you.

    Thank you.

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    3 comments

  • TS Guhan
    • Nishad Bhan (Moderator)
  • ansyori
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home