Update: There's now an official support for this type. Use @ObjectId
or @Field(type="object_id")
in your annotation to use the ObjectId / MongoId type. There's no need to use the solution below.
Also, use the latest master code from github.com/doctrine/mongodb-odm and avoid using the version on the website (it's out-dated).
Solution (deprecated)
Looks like there's no support for this yet. I discussed this issue on the IRC channel and opened a ticket for it here: https://github.com/doctrine/mongodb-odm/issues/125
A temporary fix would be to define a custom type and use an annotation like @Field(type="objectid")
in your Document classes.
Here's the code for the custom type I'm using to accomplish this.
/**
* Custom Data type to support the MongoId data type in fields
*/
class ObjectId extends \Doctrine\ODM\MongoDB\Mapping\Types\Type
{
public function convertToDatabaseValue($value)
{
if ($value === null) {
return null;
}
if ( ! $value instanceof \MongoId) {
$value = new \MongoId($value);
}
return $value;
}
public function convertToPHPValue($value)
{
return $value !== null ? (string)$value : null;
}
}
Register it using
\Doctrine\ODM\MongoDB\Mapping\Types\Type::addType('objectid', 'ObjectId' );