You are browsing a version that is no longer maintained.

Metadata Drivers

The heart of an object mapper is the mapping information that glues everything together. It instructs the DocumentManager how it should behave when dealing with the different documents.

Core Metadata Drivers

Doctrine provides a few different ways for you to specify your metadata:

  • XML files (XmlDriver)
  • Class DocBlock Annotations (AnnotationDriver)
  • Attributes (AttributeDriver)
  • PHP Code in files or static functions (PhpDriver)

Something important to note about the above drivers is they are all an intermediate step to the same end result. The mapping information is populated to Doctrine\ODM\MongoDB\Mapping\ClassMetadata instances. So in the end, Doctrine only ever has to work with the API of the ClassMetadata class to get mapping information for a document.

The populated ClassMetadata instances are also cached so in a production environment the parsing and populating only ever happens once. You can configure the metadata cache implementation using the setMetadataCacheImpl() method on the Doctrine\ODM\MongoDB\Configuration class:

1<?php $em->getConfiguration()->setMetadataCacheImpl(new ApcCache());
2
3

If you want to use one of the included core metadata drivers you just need to configure it. All the drivers are in the Doctrine\ODM\MongoDB\Mapping\Driver namespace:

1<?php $driver = new \Doctrine\ODM\MongoDB\Mapping\Driver\XmlDriver('/path/to/mapping/files'); $em->getConfiguration()->setMetadataDriverImpl($driver);
2
3
4

Implementing Metadata Drivers

In addition to the included metadata drivers you can very easily implement your own. All you need to do is define a class which implements the Driver interface:

1<?php namespace Doctrine\ODM\MongoDB\Mapping\Driver; use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; interface Driver { /** * Loads the metadata for the specified class into the provided container. * * @param string $className * @param ClassMetadata $metadata */ function loadMetadataForClass($className, ClassMetadata $metadata); /** * Gets the names of all mapped classes known to this driver. * * @return array The names of all mapped classes known to this driver. */ function getAllClassNames(); /** * Whether the class with the specified name should have its metadata loaded. * This is only the case if it is either mapped as a Document or a * MappedSuperclass. * * @param string $className * @return boolean */ function isTransient($className); }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

If you want to write a metadata driver to parse information from some file format we've made your life a little easier by providing the AbstractFileDriver implementation for you to extend from:

1<?php class MyMetadataDriver extends AbstractFileDriver { protected $_fileExtension = '.dcm.ext'; public function loadMetadataForClass($className, ClassMetadata $metadata) { $data = $this->_loadMappingFile($file); // populate ClassMetadata instance from $data } protected function _loadMappingFile($file) { // parse contents of $file and return php data structure } }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

When using the AbstractFileDriver it requires that you only have one document defined per file and the file named after the class described inside where namespace separators are replaced by periods. So if you have a document named Documents\User and you wanted to write a mapping file for your driver above you would need to name the file Documents.User.dcm.ext for it to be recognized.

Now you can use your MyMetadataDriver implementation by setting it with the setMetadataDriverImpl() method:

1<?php $driver = new MyMetadataDriver('/path/to/mapping/files'); $em->getConfiguration()->setMetadataDriverImpl($driver);
2
3
4

ClassMetadata

The last piece you need to know and understand about metadata in Doctrine is the API of the ClassMetadata classes. You need to be familiar with them in order to implement your own drivers but more importantly to retrieve mapping information for a certain document when needed.

You have all the methods you need to manually specify the mapping information instead of using some mapping file to populate it from. The base ClassMetadata class is responsible for only data storage and is not meant for runtime use. It does not require that the class actually exists yet so it is useful for describing some document before it exists and using that information to generate for example the documents themselves. The class ClassMetadata extends ClassMetadata and adds some functionality required for runtime usage and requires that the PHP class is present and can be autoloaded.

You can read more about the API of the ClassMetadata classes in the PHP Mapping chapter.

Getting ClassMetadata Instances

If you want to get the ClassMetadata instance for a document in your project to programmatically use some mapping information to generate some HTML or something similar you can retrieve it through the ClassMetadataFactory:

1<?php $cmf = $em->getMetadataFactory(); $class = $cmf->getMetadataFor(MyDocumentName::class);
2
3
4

Now you can learn about the document and use the data stored in the ClassMetadata instance to get all mapped fields for example and iterate over them:

1<?php foreach ($class->fieldMappings as $fieldMapping) { echo $fieldMapping['fieldName'] . "\n"; }
2
3
4
5