在链配置的命名空间 App\Entity\Users 中找不到类“App\Repository\Users\UserRepository”

use*_*933 0 repository symfony

我已经看到了有关此错误消息的几个问题,但答案对我没有帮助,所以我们再试一次:

我有 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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
{
...
}
Run Code Online (Sandbox Code Playgroud)

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']
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

我试图将\添加到

@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']
Run Code Online (Sandbox Code Playgroud)

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

谢谢你的帮助。

Iho*_*rov 5

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

->getRepository(User::class)
Run Code Online (Sandbox Code Playgroud)