1
votes

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?

1
Have you set UserRepository as repository of Entity\User entity in its mapping?Jakub Matczak
@dragoste Thank you! That line was commented out in my yml definition... I think I better delete this question? Unless you add your comment as an answer so I can accept it.Luke
I've posted an answer because I think it's a common issue.Jakub Matczak

1 Answers

3
votes

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