Symfony2 OAuth2提供程序错误UserRepository

时间:2013-12-19 19:02:14

标签: php symfony doctrine-orm oauth-2.0

我是Symfony框架的新手,并使用教程http://blog.tankist.de/blog/2013/07/17/oauth2-explained-part-2-setting-up-oauth2-with-symfony2-using-fosoauthserverbundle/获取错误:

ClassNotFoundException: Attempted to load class "UserRepository" from namespace "Acme\DemoBundle\Repository" in C:\xampp\htdocs\*****\vendor\doctrine\orm\lib\Doctrine\ORM\Repository\DefaultRepositoryFactory.php line 75. Do you need to "use" it from another namespace?

我认为该代码可能会导致此错误:

<!-- src/Acme/DemoBundle/Resources/config/services.xml -->
<parameters>
    <parameter key="platform.entity.user.class">Acme\DemoBundle\Entity\User</parameter>
    <parameter key="platform.user.provider.class">Acme\DemoBundle\Provider\UserProvider</parameter>
</parameters>

<services>
    <service id="platform.user.manager" class="Doctrine\ORM\EntityManager"
             factory-service="doctrine" factory-method="getManagerForClass">
        <argument>%platform.entity.user.class%</argument>
    </service>

    <service id="platform.user.repository"
             class="Acme\DemoBundle\Repository\UserRepository"
             factory-service="platform.user.manager" factory-method="getRepository">
        <argument>%platform.entity.user.class%</argument>
    </service>

    <service id="platform.user.provider" class="%platform.user.provider.class%">
        <argument type="service" id="platform.user.repository" />
    </service>
</services>

OR

<?php
  // src/Acme/DemoBundle/Provider/UserProvider.php

  namespace Acme\DemoBundle\Provider;

  use Symfony\Component\Security\Core\User\UserInterface;
  use Symfony\Component\Security\Core\User\UserProviderInterface;
  use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  use Doctrine\Common\Persistence\ObjectRepository;
  use Doctrine\ORM\NoResultException;

  class UserProvider implements UserProviderInterface
  {
      protected $userRepository;

      public function __construct(ObjectRepository $userRepository){
          $this->userRepository = $userRepository;
      }

      public function loadUserByUsername($username)
      {
          $q = $this->userRepository
              ->createQueryBuilder('u')
              ->where('u.username = :username OR u.email = :email')
              ->setParameter('username', $username)
              ->setParameter('email', $username)
              ->getQuery();

          try {
              $user = $q->getSingleResult();
          } catch (NoResultException $e) {
              $message = sprintf(
                  'Unable to find an active admin AcmeDemoBundle:User object identified by "%s".',
                  $username
              );
              throw new UsernameNotFoundException($message, 0, $e);
          }

          return $user;
      }

      public function refreshUser(UserInterface $user)
      {
          $class = get_class($user);
          if (!$this->supportsClass($class)) {
              throw new UnsupportedUserException(
                  sprintf(
                      'Instances of "%s" are not supported.',
                      $class
                  )
              );
          }

          return $this->userRepository->find($user->getId());
      }

      public function supportsClass($class)
      {
          return $this->userRepository->getClassName() === $class
          || is_subclass_of($class, $this->userRepository->getClassName());
      }
  }

但我无法理解......

3 个答案:

答案 0 :(得分:4)

我遇到了同样的问题,这就是我所做的。

实体文件夹

中创建名为 UserRepository.PHP 的文件

在里面,把这段代码

命名空间Login \ LoginBundle \ Entity;

use Doctrine\ORM\EntityRepository;

/**
 * UserRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class UserRepository extends EntityRepository
{
}

使用正确的命名空间,包名称,类名等。这对我有用,希望它也适合你。

答案 1 :(得分:3)

正确答案如下:

在src / Acme / DemoBundle /

下创建一个新文件夹Repository

使用以下代码创建新文件UserRepository.php。

<?php 
namespace Acme\DemoBundle\Repository;

use Doctrine\ORM\EntityRepository;

/**
* UserRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class UserRepository extends EntityRepository
{
}

这是纠正错误的原因,如果您发现错误,则表明未在&#34; Acme \ DemoBundle \ Repository&#34;中找到类UserRepository。因此,它只是在该目录中创建该类。

答案 2 :(得分:1)

我在Symfony2 Book

中找到了一个简单的修复程序

在项目根目录的命令行中尝试:

$ php app/console doctrine:generate:entities YourAppBundle

这将有助于使实体处于正确的顺序并可能解决问题。它对我有用。 :)