Home Blog Contact

How to create a repository in Shopware 6?

Jan 27th 2022

EDIT: Joshua Behrens has pointed out one thing very important, in a gentle way. In Shopware 6 repositories were designed to be defined on EntityDefinition class, as the official documentation explains. You might also check the blogpost I wrote about how to create a EntityDefinition. Yes, repositories can be created manually as any other service but they wouldn't be available in some parts of the system such as the API calls, for instance. Once again, kudos to Joshua Behrens for his generous contributions.

Repositories are abstraction layers which enable the application to communicate with the database (mostly the database, but any other storage like in-memory, CSV, external API... etc).

For the most of the time, I would say that: database tables are represented by repositories in Shopware 6.

Let's create a repository. Please, add the following service to the services.xml file:

<service class="Shopware\Core\Framework\DataAbstractionLayer\EntityRepository" id="matheusgontijo_speaker.repository">
    <argument type="service" id="MatheusGontijo\HelloWorld\Content\Speaker\SpeakerDefinition"/>
    <argument type="service" id="Shopware\Core\Framework\DataAbstractionLayer\Read\EntityReaderInterface"/>
    <argument type="service" id="Shopware\Core\Framework\DataAbstractionLayer\VersionManager"/>
    <argument type="service" id="Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearcherInterface"/>
    <argument type="service" id="Shopware\Core\Framework\DataAbstractionLayer\Search\EntityAggregatorInterface"/>
    <argument type="service" id="event_dispatcher"/>
    <argument type="service" id="Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEventFactory"/>
</service>

Now, let's create a command in order to write and read some data. If you are not familiar with commands, please check this blogpost I wrote recently.

<?php declare(strict_types=1);

namespace MatheusGontijo\HelloWorld\Command;

use MatheusGontijo\HelloWorld\Content\Speaker\SpeakerCollection;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;


class MakeSureRepositoryWorksCommand extends Command
{
    protected static $defaultName = 'matheus-gontijo:make-sure-repository-works';

    protected EntityRepositoryInterface $speakerRepository;

    public function __construct(
        EntityRepositoryInterface $speakerRepository,
        string $name = null
    ) {
        parent::__construct($name);
        $this->speakerRepository = $speakerRepository;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        for ($i = 1; $i <= 10; $i++) {
            $this->speakerRepository->create([[
                'id'          => Uuid::randomHex(),
                'name'        => 'Matheus Gontijo ' . rand(0, 999),
                'nationality' => 'Brazilian',
            ]], Context::createDefaultContext());
        }

        /** @var SpeakerCollection $speakers */
        $speakers = $this->speakerRepository->search(new Criteria(), Context::createDefaultContext());

        foreach ($speakers as $speaker) {
            $output->writeln($speaker->getName());
        }

        return self::SUCCESS;
    }
}

Let's call the command line:

php bin/console matheus-gontijo:make-sure-repository-works

Result:

Matheus Gontijo 826
Matheus Gontijo 996
Matheus Gontijo 827
Matheus Gontijo 263
Matheus Gontijo 790
Matheus Gontijo 867
Matheus Gontijo 250
Matheus Gontijo 97
Matheus Gontijo 783
Matheus Gontijo 48

Fantastic, it is working! There we go, I can write and read data from my speaker repository!