'The class 'App\Repository\Users\UserRepository' was not found in the chain configured namespaces App\Entity\Users

I've seen a few questions about this error message but the answers weren't helpful to me so here we go again:

I have 2 configured entity managers and when I call

dump($this->getManager('db_users')->getRepository(UserRepository::class));

it throws the error

The class 'App\Repository\Users\UserRepository' was not found in the chain configured namespaces App\Entity\Users.

So it's the repository problem (or isn't it? Symfony likes to throw errors absolutely not related to the real problem).

List of my files:

UserRepository.php (nothing special... just added \Users to the namespace to be the same as real directory path)

<?php

declare(strict_types=1);

namespace App\Repository\Users;

use App\Entity\Users\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
use Symfony\Component\Security\Core\User\UserInterface;

class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, User::class);
    }

    /**
     * Used to upgrade (rehash) the user's password automatically over time.
     */
    public function upgradePassword(UserInterface $user, string $newEncodedPassword): void
    {
        if (!$user instanceof Uzytkownik) {
            throw new \Exception(sprintf('Instances of "%s" are not supported.', \get_class($user)));
        }

        $user->setPassword($newEncodedPassword);
        $this->_em->persist($user);
        $this->_em->flush();
    }
}

User.php (everything here is valid, I'm sure. User.php is inside src/Entity/Users/)

<?php

declare(strict_types=1);

namespace App\Entity\Users;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\{UserInterface, EquatableInterface};
use Symfony\Component\Validator\Constraints as Assert;

/** 
 * @ORM\Entity(repositoryClass="App\Repository\Users\UserRepository")
 * @ORM\Table(name="t_users")
 */
class User implements UserInterface, EquatableInterface, \Serializable
{
...
}

Services.yaml (standard... I see all my repos under bin/console debug:container command so it seems like I don't need to configure repositories here)

services:
    _defaults:
        autowire: true
        autoconfigure: true

    App\:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

doctrine.yaml (also perfectly configured as the documentation suggests: https://symfony.com/doc/current/doctrine/multiple_entity_managers.html)

orm:
    default_entity_manager: db_users
    
    entity_managers:
        db_users:
            connection: db_users
            mappings:
                User:
                    is_bundle: false
                    type: annotation
                    dir: '%kernel.project_dir%/src/Entity/Users'
                    prefix: 'App\Entity\Users'
                    alias: User

        db_core:
            connection: db_core
            mappings:
                Core:
                    is_bundle: false
                    type: annotation
                    dir: '%kernel.project_dir%/src/Entity/Core'
                    prefix: 'App\Entity\Core'
                    alias: Core

I tried to add \\ to

@ORM\Entity(repositoryClass="App\Repository\Users\UserRepository") so result would be

@ORM\Entity(repositoryClass="\App\Repository\Users\UserRepository")

but it is not suggested probably and it also comes with such error:

The "\App\Repository\Users\UserRepository" entity repository implements "Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface", but its service could not be found. Make sure the service exists and is tagged with "doctrine.repository_service.

Well then I tagged it like this:

App\Repository\Users\:
    resource: '../src/Repository/Users'
    tags: ['doctrine.repository_service']

App\Repository\Core\:
    resource: '../src/Repository/Core'
    tags: ['doctrine.repository_service']

and the same error occurred even when I deleted var folder :)

Thanks for any help.



Solution 1:[1]

You need to pass the entity class name, instead of the repository class name, like this:

->getRepository(User::class)

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 Marcin Orlowski