skip to Main Content

In Sylius 1.11, after creating a new Campaign entity using the maker bundle, I get this error when trying to fetch a campaign using its repository:

Cannot autowire service "AppRepositoryCampaignRepository": argument "$class" of method "DoctrineORMEntityRepository::__construct()" references class "DoctrineORMMappingClassMetadata" but no such service exists.

This seems to be the code that trigger the error:

<?php

namespace AppController;

use AppRepositoryCampaignRepository;

class CampaignController extends AbstractController {
    protected CampaignRepository $repository;

    public function __construct(CampaignRepository $repository) {
        $this->repository = $repository;
    }

    public function details(string $id)
    {
        $campaign = $this->repository->find($id);

        dd($campaign);
    }
}

The AppRepositoryCampaignRepository exists and is defined as follow, which is what the Sylius documentation recommends:

<?php

namespace AppRepository;

use AppEntityCampaign;
use DoctrineBundleDoctrineBundleRepositoryServiceEntityRepository;
use SyliusBundleResourceBundleDoctrineORMEntityRepository;

/**
 * @extends ServiceEntityRepository<Campaign>
 *
 * @method Campaign|null find($id, $lockMode = null, $lockVersion = null)
 * @method Campaign|null findOneBy(array $criteria, array $orderBy = null)
 * @method Campaign[]    findAll()
 * @method Campaign[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class CampaignRepository extends EntityRepository
{
}

How to fix this error?

3

Answers


  1. Chosen as BEST ANSWER

    TL;DR

    It turns out Sylius is quite picky on both the type hinting and the variable name. This is due to how Symfony's dependency injection works and how Sylius chose to use it.

    Change the constructor to:

    public function __construct(
       SyliusComponentResourceRepositoryRepositoryInterface $campaignRepository
    ) {
        $this->repository = $campaignRepository;
    }
    

    How are you supposed to guess that?

    You need to use the bin/console debug:container command. But if you try, you'll run into the same error message as when running you controller action.

    What you need to do is:

    1. comment any method that use the symfony's dependency injection feature to access an instance of your repository (in our case the AppControllerCampaignController::__construct() method)
    2. run the bin/console debug:container CampaignRepository
      This will return you a list of services:
    bin/console debug:container CampaignRepository
    
    Select one of the following services to display its information:
    [0] AppRepositoryCampaignRepository
    [1] DoctrinePersistenceObjectRepository $campaignRepository
    [2] DoctrineCommonCollectionsSelectable $campaignRepository
    [3] SyliusComponentResourceRepositoryRepositoryInterface $campaignRepository
    [4] SyliusBundleResourceBundleDoctrineORMEntityRepository $campaignRepository
    [5] DoctrineORMEntityRepository $campaignRepository
    
    1. chose a combination of class name & parameter to use as your constructor parameter (I went with SyliusComponentResourceRepositoryRepositoryInterface $campaignRepository but any of the suggestions with a parameter name should work)
    2. uncomment the method you commented on step 1 & update its signature
    3. voilà

    If you're curious how this works, have a look here: https://symfony.com/doc/current/service_container/autowiring.html#dealing-with-multiple-implementations-of-the-same-type


  2. As a type-hint you can use any of the interfaces that the repository class implements, the base ones that can be used for any resource repository are SyliusComponentResourceRepositoryRepositoryInterface and DoctrinePersistenceObjectRepository. For built-in Sylius resources, there are repository interfaces in the lower-level components and sometimes in the core component, like SyliusComponentOrderRepositoryOrderRepositoryInterface and SyliusComponentCoreRepositoryOrderRepositoryInterface.
    Plugins usually also define interfaces.
    For custom repositories in your projects, you should also define interfaces if you add custom methods.
    The argument name also matters.
    To check which type-hints are available, together with the argument name use the command bin/console debug:autowiring campaing.

    Login or Signup to reply.
  3. you can add to the file services.yaml

    your sercvice here -> the path of your file: autowire: true
    or add the arguments manually
    your sercvice here -> the path of your file: arguments:[....]

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search