Luke
Luke

Reputation: 21236

Unable to inject Entity Repository into service

I'm currently setting up an application with Symfony 3 and Doctrine 2.5 and I'm trying to inject an entity repository into a service and I keep getting the following error:

Type error: Argument 1 passed to UserService::setUserRepository() must be an instance of UserRepository, instance of Doctrine\ORM\EntityRepository given, called in appDevDebugProjectContainer.php on line 373

This is how wire things up in my services.yml:

service_user:
  class: UserService
  calls:
    - [setUserRepository, ["@service_user_repository"]]

service_user_repository:
  class: UserRepository
  factory: ["@doctrine.orm.entity_manager", getRepository]
  arguments: [Entity\User]

This is my UserService:

<?php

class UserService {

    protected $userRepository;

    public function setUserRepository( UserRepository $userRepository )
    {
        $this->userRepository = $userRepository;
    }

}

And this is my UserRepository:

<?php

use Doctrine\ORM\EntityRepository;

class UserRepository extends EntityRepository {

}

I have checked and double checked my namespaces and class names, all seem to check out fine.

How do I inject an entity repository into a service with Symfony 3 service wiring?

Upvotes: 1

Views: 491

Answers (1)

Jakub Matczak
Jakub Matczak

Reputation: 15656

As mentioned in comment, everything you've showed looks fine.

But since you're getting from entity manager an EntityRepository instance instead of UserRepository, it means that you didn't configured User entity to have custom (UserRepository) repository class.

If you use YAML mapping, it should be something like:

Entity\User:
    repositoryClass: UserRepository
    # rest of mapping

Upvotes: 3

Related Questions