1

I try to injcect doctrine mongo repository to controller. In services.yaml file I added entry:

    App\Account\Repository\MongoAccountRepository:
    factory: ["@doctrine_mongodb", getRepository]
    arguments:
      - App\Account\Domain\Entity\Account

In my code I want to use repositories hidden behind the interface AccountRepository

class MongoAccountRepository extends DocumentRepository implements AccountRepository {}

When I try to inject repository to controller constructor

class DefaultController extends Controller 
{
     private $accountRepository;

     public function __construct(AccountRepository $accountRepository) {
           $this->accountRepository = $accountRepository;
     }

I get following error:

Argument 1 passed to App\Account\UserInterface\DefaultController::__construct() must implement interface App\Account\Domain\Repository\AccountRepository, instance of Doctrine\ODM\MongoDB\DocumentRepository given

Has someone similar problem?

Papub
  • 75
  • 1
  • 9
  • Never had this problem, but it seems like your controller receive a `DocumentRepository`, not a `MongoAccountRepository` and as your `DocumentRepository` doesn't implement `AccountRepository`. what's your factory does to create `MongoAccountRepository` ? – Etshy Feb 11 '19 at 11:00
  • My mongo repository implements AccountRepository. (class MongoAccountRepository extends DocumentRepository implements AccountRepository). For test when I change in controller it to DocumentRepository I get: Cannot autowire service "App\Account\UserInterface\Api\DefaultController": argument "$accountRepository" of method "__construct()" references class "Doctrine\ODM\MongoDB\DocumentRepository" but no such service exists. You should maybe alias this class to one of these existing services: "App\Account\Infrastructure\Persistence\Doctrine\MongoAccountRepository", – Papub Feb 11 '19 at 21:27
  • Did you try adding a definition for your interface ? to map the Interface FQCN to an actual class ? https://symfony.com/doc/current/service_container/autowiring.html#working-with-interfaces. It seems you could make something like that : `yourFQCN/AccountRepository : '@App\Account\Repository\MongoAccountRepository'` – Etshy Feb 12 '19 at 08:32
  • I have probably right :) I figure out this today morning :D I will check it today and let you know :) – Papub Feb 12 '19 at 09:32
  • Unfortunately it seems that nothing changed. `app.repository.mongo: class: Doctrine\ODM\MongoDB factory: ["@doctrine_mongodb", getRepository] arguments: - App\Account\Domain\Entity\Account App\Account\Domain\Repository\AccountRepository: '@app.repository.mongo` – Papub Feb 14 '19 at 20:33
  • Hmm I really don't know. in my case I did that inside a CompilerPass because I needed some logic but it should be equivalent. As you're using a Factory (it seems) try changing your `app.repository.mongo` definition to something like this `app.repository.mongo : factory: 'Doctrine\ODM\MongoDB factory:getRepository'` (I guess `getRepository` is your factory function). see here : https://symfony.com/doc/current/service_container/factories.html – Etshy Feb 15 '19 at 08:39

1 Answers1

1

For all my cases, the following solution works:

mongo_account_repository:
    class: Doctrine\ODM\MongoDB\Repository\DocumentRepository
    factory: ['@doctrine_mongodb.odm.default_document_manager', getRepository]
    arguments:
        - App\Infrastructure\Repository\MongoDB\Document\Keyword
mcek
  • 480
  • 7
  • 17