XML 格式的 Doctrine ODM 捆绑文档映射

Doctrine ODM bundle document mapping in XML format

我在 Symfony 4 中使用 Doctrine ODM 包。 尝试对文档使用 XML 映射格式并出现以下错误:

No mapping file found named 'Post.mongodb.xml' for class 'App\Document\Post'.

我的doctrine_mongodb.yaml文件:

doctrine_mongodb:
    auto_generate_proxy_classes: '%kernel.debug%'
    auto_generate_hydrator_classes: '%kernel.debug%'
    connections:
        default:
            server: '%env(MONGODB_URL)%'
            options: {}
    default_database: '%env(MONGODB_DB)%'
    document_managers:
        default:
            auto_mapping: true
            mappings:
                App:
                    is_bundle: false
                    type: xml
                    dir: '%kernel.project_dir%/src/Resources/config/doctrine'
                    prefix: App\Document\
                    alias: App

文档src/Document/Post.php文件:

namespace App\Document;

class Post
{
    protected $id;

    protected $title;

    protected $text;

//Getters and setters
}

XML映射文件src/Resources/config/doctrine/Post.mongodb.xml:

<doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping"
                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
                    http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">

    <document name="App\Document\Post">
        <field fieldName="id" id="true" />
        <field fieldName="title" type="string" />
        <field fieldName="text" type="string" />
    </document>
</doctrine-mongo-mapping>

作曲家配置(需要部分): "require": { "php": "^7.1.3", "ext-ctype": "*", "ext-iconv": "*", "alcaeus/mongo-php-adapter": "^1.1", "doctrine/mongodb-odm-bundle": "^3.5", "symfony/console": "4.2.*", "symfony/dotenv": "4.2.*", "symfony/flex": "^1.1", "symfony/framework-bundle": "4.2.*", "symfony/yaml": "4.2.*" } 有什么想法吗?

将映射文件命名为 Post.dcm.xml

此约定由 design 制定。对于 App\Document\Namespace\Another 文档,它将是 Namespace.Another.dcm.xml.

prefix 值中执行尾部斜杠的问题,我的配置中的一切都是正确的。

根据 documentation:

The name of the mapping document must consist of the fully qualified name of the class, where namespace separators are replaced by dots (.).

如果您提供前缀,即文档的命名空间,您将获得类似 Post.mongodb.xml

的映射文件名

示例:

  • Post 文档的命名空间 App\Document
  • 你的映射前缀App\Document
  • 分别映射文件名Post.mongodb.xml