如何在自定义包中使用存储库?

时间:2019-12-05 19:06:17

标签: php symfony doctrine-orm symfony4

我正在为Symfony 4构建一个可重用的捆绑包,每5分钟就会卡住一次,这次是关于教义的。

我一直(喜欢)这个doc

在我的捆绑包中,我有一个用户实体及其存储库,首先,我用php bin/console make:user创建了它们,然后用php bin/console make:auth创建了一个简单的登录示例,当所有类和服务都在其中时,它可以工作App\名称空间,但我需要在捆绑包中添加一些内容,例如实体和存储库。

当我仅移动实体并重写实体及其存储库的名称空间时,它就起作用了,但是当我将存储库移动到我的捆绑软件中以将它们都包含在捆绑软件中时,会出现此错误:

  

“ ExampleVendor \ AdminBundle \ Repository \ UserRepository”实体存储库实现了“ Doctrine \ Bundle \ DoctrineBundle \ Repository \ ServiceEntityRepositoryInterface”,但是找不到其服务。确保该服务存在,并标记为“ doctrine.repository_service”。

这是我的存储库类,它是自动生成的。

<?php

namespace ExampleVendor\AdminBundle\Repository;

use ExampleVendor\AdminBundle\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * @method User|null find($id, $lockMode = null, $lockVersion = null)
 * @method User|null findOneBy(array $criteria, array $orderBy = null)
 * @method User[]    findAll()
 * @method User[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
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 User) {
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user)));
        }

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

该错误说明了某个ServiceEntityRepositoryInterface的问题,但是存储库没有直接实现它,而是扩展了一个ServiceEntityRepository,我猜这是实现ServiceEntityRepositoryInterface的那个人,所以我应该注入{ {1}}到我的存储库中?怎么做?我该怎么办?

1 个答案:

答案 0 :(得分:1)

2 + '3' = '23'中的自动装配和自动配置可能不包括您的自定义包路径。因此,您需要手动配置存储库并自动对其进行接线:

services.yaml