'getUser method from Symfony Security can't get my User Repository

My problem is the $this->getUser(); method (called from Controller) from Symfony Security can't get my User Repository.

The "App\Model\ODM\Repository\UserRepository" document repository implements "Doctrine\Bundle\MongoDBBundle\Repository\ServiceDocumentRepositoryInterface", but its service could not be found. Make sure the service exists and is tagged with "doctrine_mongodb.odm.repository_service".

I have a CompilerPass that get my Repositories and add them to Container Definitions depending on some env value. I add the Repo with "generic" Interfaces (valid for both ODM and ORM) as keys AND with the Repo FQCN as keys, so they should be accessible via both the generic Interface and the true Repo name, but I'm working only with ODM for now. Also, I "tag" my repo with doctrine_mongodb argument, to extends the ServiceDocumentRepository baseClass.

I don't include the compilerPass because the class is quite "heavy", but if needed I'll include it.

Here is my UserRepo

class UserRepository extends BaseRepository implements RepositoryInterface, UserRepositoryInterface
{
    /**
     * UserRepository constructor.
     *
     * @param ManagerRegistry $managerRegistry
     */
    public function __construct(ManagerRegistry $managerRegistry)
    {
        parent::__construct($managerRegistry, User::class);
    }

...

My BaseRepo (just a few methods in here, just the extends is interesting)

abstract class BaseRepository extends ServiceDocumentRepository implements RepositoryInterface
{
 ...

It's working great "in-app", I could create a User on a registration page, but using $guardHandler->authenticateUserAndHandleSuccess() method after register, i'm redirected to my homepage where the $this->getUser(); is, and then I have the error message posted above.

After some research, it seems the problem is in Doctrine\Bundle\MongoDBBundle\Repository\ContainerRepositoryFactory

The container of this class doesn't seem to have any of my Repo definition.

I surely missed something but I don't know where. I remember making something similar few years ago but extending DocumentRepository and not ServiceDocumentRepository.

If you need any other information, let me know.



Solution 1:[1]

I feel really dumb about this question now that I found the solution ...

It was LITTERALY in the error message, I just had to add doctrine_mongodb.odm.repository_service to my Repo's Definition, in my Compiler Pass

...
$definition->addArgument(new Reference('doctrine_mongodb'));
$definition->addTag('doctrine_mongodb.odm.repository_service');
...

Thanks to the people who looked at my problem and sorry for wasting your time.

I Hope this answer could help others peoples that register Repo in a CompilerPass

(I will set this answer as the solution asap, in 2 days)

Solution 2:[2]

Did you enable autowire in your services.yml?

services:
    _defaults:
        autowire: true
        autoconfigure: true

    App\Repository\:
        resource: '../src/Repository/*'

Solution 3:[3]

Ill put my solution in case someone else has this problem and the one above doesn't work. For me what worked was to put the whole path to the repository when it was declared in the Document class. Like so:

// /src/Document/User

/**
 * @MongoDB\Document(collection="users", db="database", repositoryClass=\App\Repository\UserRepository::class)
 * @MongoDBUnique(fields="email")
 */
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
    

I didn'nt have to add anything in my Compiler Pass. I added the full path to the repository class in its declaration and it instantly worked. Hope that it helps.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Etshy
Solution 2 Adil
Solution 3 AntonioMMartel