在链配置的命名空间 App\Entity\Users 中找不到 class 'App\Repository\Users\UserRepository'

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

我看到了一些关于此错误消息的问题,但答案对我没有帮助,所以我们再来一次:

我有 2 个已配置的实体管理器,当我调用时

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

它抛出错误

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

所以这是存储库问题(或者不是吗?Symfony 喜欢抛出与实际问题完全无关的错误)。

我的文件列表:

UserRepository.php(没什么特别的...只是将 \Users 添加到名称空间以与实际目录路径相同)

<?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(这里的一切都是有效的,我敢肯定。User.php在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(标准...我在 bin/console debug:container 命令下看到了我所有的存储库,所以我似乎不需要在这里配置存储库)

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(也按照文档建议完美配置: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

我尝试将 \ 添加到

@ORM\Entity(repositoryClass="App\Repository\Users\UserRepository") 所以结果会是

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

但可能不建议,它也有这样的错误:

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.

那么我就这样标记了它:

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

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

即使我删除了 var 文件夹,也会出现同样的错误:)

感谢您的帮助。

您需要传递 实体 class 名称,而不是存储库 class 名称,如下所示:

->getRepository(User::class)