在链配置的命名空间 FOS\OAuthServerBundle\Document 中找不到 class 'OAuthBundle\Entity\Client'

The class 'OAuthBundle\Entity\Client' was not found in the chain configured namespaces FOS\OAuthServerBundle\Document

我在文档中做了 FOSOAuthServer https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/blob/master/Resources/doc/index.md

但有例外:

Doctrine\Common\Persistence\Mapping\MappingException:
The class 'OAuthBundle\Entity\Client' was not found in the chain configured namespaces FOS\OAuthServerBundle\Document

  at vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\MappingException.php:37
  at Doctrine\Common\Persistence\Mapping\MappingException::classNotFoundInNamespaces('OAuthBundle\Entity\Client', array('FOS\OAuthServerBundle\Document'))
     (vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain.php:112)
  at Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain->loadMetadataForClass('OAuthBundle\Entity\Client', object(ClassMetadata))
     (vendor\doctrine\mongodb-odm\lib\Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory.php:159)
  at Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory->doLoadMetadata(object(ClassMetadata), object(ClassMetadata), false, array())
     (vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory.php:333)
  at Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->loadMetadata('OAuthBundle\Entity\Client')
     (vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory.php:217)
  at Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor('OAuthBundle\Entity\Client')
     (vendor\doctrine\mongodb-odm\lib\Doctrine\ODM\MongoDB\DocumentManager.php:286)
  at Doctrine\ODM\MongoDB\DocumentManager->getClassMetadata('OAuthBundle\Entity\Client')
     (vendor\doctrine\mongodb-odm\lib\Doctrine\ODM\MongoDB\Repository\DefaultRepositoryFactory.php:24)
  at Doctrine\ODM\MongoDB\Repository\DefaultRepositoryFactory->getRepository(object(DocumentManager), 'OAuthBundle\Entity\Client')
     (vendor\doctrine\mongodb-odm\lib\Doctrine\ODM\MongoDB\DocumentManager.php:508)
  at Doctrine\ODM\MongoDB\DocumentManager->getRepository('OAuthBundle\Entity\Client')
     (vendor\friendsofsymfony\oauth-server-bundle\Document\ClientManager.php:39)
  at FOS\OAuthServerBundle\Document\ClientManager->__construct(object(DocumentManager), 'OAuthBundle\Entity\Client')
     (var\cache\dev\appDevDebugProjectContainer.php:1780)
  at appDevDebugProjectContainer->getFosOauthServer_ClientManager_DefaultService()
     (vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Container.php:335)
  at Symfony\Component\DependencyInjection\Container->get('fos_oauth_server.client_manager.default')
     (var\cache\dev\appDevDebugProjectContainer.php:1820)
  at appDevDebugProjectContainer->getFosOauthServer_StorageService()
     (vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Container.php:335)
  at Symfony\Component\DependencyInjection\Container->get('fos_oauth_server.storage')
     (var\cache\dev\appDevDebugProjectContainer.php:1810)
  at appDevDebugProjectContainer->getFosOauthServer_ServerService()
     (vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Container.php:335)
  at Symfony\Component\DependencyInjection\Container->get('fos_oauth_server.server')
     (var\cache\dev\appDevDebugProjectContainer.php:1790)
  at appDevDebugProjectContainer->getFosOauthServer_Controller_TokenService()
     (vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Container.php:335)
  at Symfony\Component\DependencyInjection\Container->get('fos_oauth_server.controller.token')
     (vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Controller\ContainerControllerResolver.php:54)
  at Symfony\Component\HttpKernel\Controller\ContainerControllerResolver->createController('fos_oauth_server.controller.token:tokenAction')
     (vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver.php:52)
  at Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver->createController('fos_oauth_server.controller.token:tokenAction')
     (vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Controller\ControllerResolver.php:95)
  at Symfony\Component\HttpKernel\Controller\ControllerResolver->getController(object(Request))
     (vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Controller\TraceableControllerResolver.php:58)
  at Symfony\Component\HttpKernel\Controller\TraceableControllerResolver->getController(object(Request))
     (vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php:136)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php:68)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Kernel.php:171)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (web\app_dev.php:29)
  at require('C:\www\symfony\web\app_dev.php')
     (vendor\symfony\symfony\src\Symfony\Bundle\WebServerBundle\Resources\router.php:42)

我的实体放在 /src/OAuthBundle/Entity

客户

namespace OAuthBundle\Entity;


use Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations\Id;
use FOS\OAuthServerBundle\Document\Client as BaseClient;

/** @Document(collection="oauth_client") */
class Client extends BaseClient
{
    /** @Id(strategy="AUTO") */
    protected $id;
}

访问令牌

namespace OAuthBundle\Entity;


use Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations\Id;
use Doctrine\ODM\MongoDB\Mapping\Annotations\ReferenceOne;
use FOS\OAuthServerBundle\Document\AccessToken as BaseAccessToken;
use FOS\OAuthServerBundle\Model\ClientInterface;

/**
 * @Document(collection="oauth_access_token")
 */
class AccessToken extends BaseAccessToken
{
    /**
     * @Id(strategy="AUTO")
     */
    protected $id;

    /**
     * @ReferenceOne(targetDocument="OAuthBundle\Entity\Client")
     */
    protected $client;

    public function getClient()
    {
        return $this->client;
    }

    public function setClient(ClientInterface $client)
    {
        $this->client = $client;
    }
}

像这样的其他实体 RefreshToken 和 AuthCode。

应用配置:

doctrine_mongodb:
    connections:
        default:
            server: "%mongodb_server%"
            options: {}
    default_database: symfony_test
    document_managers:
        default:
            auto_mapping: true

fos_oauth_server:
    db_driver: mongodb
    client_class:        OAuthBundle\Entity\Client
    access_token_class:  OAuthBundle\Entity\AccessToken
    refresh_token_class: OAuthBundle\Entity\RefreshToken
    auth_code_class:     OAuthBundle\Entity\AuthCode

我的composer.json

"autoload": {
    "psr-4": {
        "AppBundle\": "src/AppBundle",
        "OAuthBundle\": "src/OAuthBundle"
    },
    "classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
},

控制台:

$ composer dumpautoload
Generating autoload files


$ ./bin/console cache:clear --env=dev

 // Clearing the cache for the dev environment with debug
 // true


 [OK] Cache for the "dev" environment (debug=true) was successfully cleared.

我不明白问题在哪里?

默认情况下,auto_mapping 功能在 Entity 命名空间下查找实体,因此假设您的实体 (FOS\OAuthServerBundle\Document\XXX) 不存在,Doctrine 对此一无所知。

您需要在您的学说映射中手动配置 FOSOAuthServerBundle 的实体,以添加您的自定义实体命名空间。

entity_managers:
        default:
            mappings:
                MyBundle:
                    type: annotation
                custom_mapping:
                    type: annotation
                    prefix: FOS\OAuthServerBundle\Document\
                    dir: "%kernel.root_dir%/src/FOS/OAuthServerBundle/Document/"
                    is_bundle: false

此配置有效:

doctrine_mongodb:
    connections:
        default:
            server: "%mongodb_server%"
            options: {}
    default_database: symfony_test
    document_managers:
        default:
            auto_mapping: true
            mappings:
                custom_mapping:
                    type: annotation
                    prefix: OAuthBundle\Entity
                    dir: "%kernel.root_dir%/../src/OAuthBundle/Entity"
                    is_bundle: false