我想访问我的数据库,其中包含我的提供程序中包含doctrine的所有用户。我按照教程(http://symfony.com/doc/current/security/custom_provider.html)为我的用户构建了我的提供程序,所以我有一个loadUserByUsername函数:
public function loadUserByUsername($username)
{
// make a call to your webservice here
$player = new Player();
$player = $this->getDoctrine()
->getRepository('AppBundle:Player')
->findOneByPseudo($username);
// pretend it returns an array on success, false if there is no user
if ($player) {
return $player;
}
throw new UsernameNotFoundException(
sprintf('Username "%s" does not exist.', $username)
);
}

但是我的getDoctrine()函数当然是未定义的。因此,我不了解提供商,我尝试在登录时使用它进行身份验证,因此我需要提供商,但为什么我无法在我的数据库中进行搜索?我应该怎么写这个函数?谢谢你的帮助
编辑: 当我通过service.yml添加doctrine时(以及在我的提供程序中写入构造函数之后),我有这个错误:
FatalThrowableError in PlayerProvider.php line 13:
Type error: Argument 1 passed to AppBundle\Security\PlayerProvider::__construct() must be an instance of Doctrine\Bundle\DoctrineBundle\Registry, instance of Doctrine\ORM\EntityManager given, called in /home/jean/PW6/SkA/SkeletonsOnlineV2/skeleton-online/var/cache/dev/appDevDebugProjectContainer.php on line 327
编辑2:当我在我的service.yml中输入参数:[' @ doctrine']时,我收到一条错误,指出该教义未定义
编辑3:它现在有效,我只是犯了一个愚蠢的错误
答案 0 :(得分:3)
如果你进一步阅读,它会说明以下内容(强调我的):
用户提供商的实际实施可能具有某些依赖性或配置选项或其他服务。 在服务定义中添加这些参数。
所以在你的情况下,它会像
# app/config/services.yml
services:
app.webservice_user_provider:
class: AppBundle\Security\User\WebserviceUserProvider
arguments: ['@doctrine']
你的班需要一个构造函数
class WebserviceUserProvider implements UserProviderInterface
{
protected $doctrine;
public function __construct (\Doctrine\Bundle\DoctrineBundle\Registry $doctrine)
{
$this->doctrine = $doctrine;
}
// ...
}
然后在您的方法中,使用$this->getDoctrine()
$this->doctine