Search code examples
mongodbsymfonydoctrine-ormdoctrine-odmodm

Doctrine ODM bundle document mapping in XML format


I'm using Doctrine ODM bundle with Symfony 4. Trying to use XML mapping format for Documents and getting the following error:

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

My doctrine_mongodb.yaml file:

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

Document src/Document/Post.php file:

namespace App\Document;

class Post
{
    protected $id;

    protected $title;

    protected $text;

//Getters and setters
}

XML mapping file 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>

Composer configuration (require section): "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.*" } Any ideas?


Solution

  • Everything was correct in my configs from the question execpt trailing slash in prefix value.

    According to the documentation:

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

    If you provide prefix, which is the namespace to your documents, you will get mapping file name like Post.mongodb.xml

    Example:

    • Namespace of the Post document App\Document
    • Your mapping prefix App\Document
    • Mapping filename Post.mongodb.xml respectively.