You are browsing a version that is no longer maintained. |
Custom Mapping Types
Doctrine allows you to create new mapping types. This can come in handy when you're missing a specific mapping type or when you want to replace the existing implementation of a mapping type.
In order to create a new mapping type you need to subclass
Doctrine\ODM\MongoDB\Types\Type and implement/override
the methods. Here is an example skeleton of such a custom type
class:
<?phpnamespace My\Project\Types;use Doctrine\ODM\MongoDB\Types\ClosureToPHP;use Doctrine\ODM\MongoDB\Types\Type;use MongoDB\BSON\UTCDateTime;/** * My custom datatype. */class MyType extends Type{ // This trait provides default closureToPHP used during data hydration use ClosureToPHP; public function convertToPHPValue($value): \DateTime { // This is called to convert a Mongo value to a PHP representation return new \DateTime('@' . $value->sec); } public function convertToDatabaseValue($value): UTCDateTime { // This is called to convert a PHP value to its Mongo equivalent return new UTCDateTime($value); }}
Restrictions to keep in mind:
- If the value of the field is NULL the method
convertToDatabaseValue()is not called. - The
UnitOfWorknever passes values to the database convert method that did not change in the request.
When you have implemented the type you still need to let Doctrine know about it:
<?php// in bootstrapping code// ...use Doctrine\ODM\MongoDB\Types\Type;// ...// Adds a type. This results in an exception if type with given name is already registeredType::addType('mytype', \My\Project\Types\MyType::class);// Overrides a type. This results in an exception if type with given name is not registeredType::overrideType('mytype', \My\Project\Types\MyType::class);// Registers a type without checking whether it was already registeredType::registerType('mytype', \My\Project\Types\MyType::class);
As can be seen above, when registering the custom types in the configuration you specify a unique name for the mapping type and map that to the corresponding |FQCN|. Now you can use your new type in your mapping like this:
<?phpclass MyPersistentClass{ /** @Field(type="mytype") */ private $field;}
