Papub
Papub

Reputation: 85

Symfony4 MongoDB inject repository

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?

Upvotes: 1

Views: 591

Answers (1)

mcek
mcek

Reputation: 490

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

Upvotes: 1

Related Questions