We use cookies to make your experience better. To comply with the new e-Privacy directive, we need to ask for your consent to set the cookies. Learn more.
How to create a repository in Shopware 6?
Before learning How to create a repository in Shopware6, let’s check out what are repositories in Shopware6.
What are the repositories in Shopware 6?
Simply stating: repositories are database tables in Showare6.
In reality, it is not so straightforward, but that is the reality for the most part.
Repositories are the way to go if you need to work with data from a database, either read or write it. You can, of course, gain direct access to the database and run your own SQL code, but this should only be used as a last option because you risk missing something and breaking things.
Above the database, repositories provide a pleasant abstraction layer that verifies all dependencies and assures data integrity. Repositories should be able to let you accomplish whatever you need with the data while being secure and compliant with the Shopware 6 data structure.
How to create the repositories
You must have a repository available in your class to be able to deal with it. This is accomplished through dependency injection in the class's function. Here's a small explanation of how we may use the product and customer repositories in our class:
Let's take the HelloWorldController controller that we had created in this blog. Modify the controller as follows.
<?php declare(strict_types=1);
namespace MageSpark\HelloWorld\Controller;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @RouteScope (scopes={"storefront"})
*/
class HelloWorldController extends StorefrontController
{
protected $productRepository;
protected $msblogRepository;
public function __construct(
EntityRepositoryInterface $productRepository,
EntityRepositoryInterface $msblogRepository
)
{
$this->productRepository = $productRepository;
$this->msblogRepository = $msblogRepository;
}
/**
* @param Request $request
* @param SalesChannelContext $salesChannelContext
*
* @return Response
* @RouteScope (scopes={"storefront"})
* @Route ("/hello-world", name="frontend.say.helloworld", methods={"GET"})
*/
public function sayHelloWorld(Request $request, SalesChannelContext $salesChannelContext): Response
{
$name = "MageSpark HelloWorld Plugin";
$time = date("F j, Y, g:i a");
$products = $this->productRepository->search(new Criteria(), $salesChannelContext->getContext());
$blogs = $this->msblogRepository->search(new Criteria(), $salesChannelContext->getContext());
return $this->renderStorefront("@MageSparkHelloWorld/storefront/page/hello-world.html.twig", [
"name" => $name,
"time" => $time,
"total_products" => $products->count(),
"total_blogs" => $blogs->count()
]);
}
}
Note the highlighted lines in above code. We have added a construct method in our controller and passed two arguments to create repositories for product and MsBlog Entity. In the sayHelloWorld method we fetch all data from product and magespark_blogs and store it in variables $products and $blogs respectively. At last, We have passed the total number of products and blogs in a view file.
Additionally, in the services.xml file, which is usually located in the custom/plugins/MageSparkHelloWorld/src/Resources/config directory, we must specify the repositories that we will inject into the class. Note the highlighted lines in the following code.
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="MageSpark\HelloWorld\Controller\HelloWorldController" public="true">
<argument type="service" id="product.repository" />
<argument type="service" id="magespark_blogs.repository"/>
<call method="setContainer">
<argument type="service" id="service_container"/>
</call>
</service>
<service id="MageSpark\HelloWorld\Core\Content\MSBlog\MSBlogsDefinition">
<tag name="shopware.entity.definition" entity="magespark_blogs" />
</service>
</services>
</container>
And so we now have access to those 2 repositories all through the class, which we can use to retrieve or save data to the respective database tables. You can modify your view file to print the total number of products and blogs. You will get results like this.
This introduction to repositories in Shopware 6 should have helped you gain an understanding. I recommend the following chapters from the official Shopware 6 manual for further information