How can I install my own MongoId with Doctrine ODM for MongoDB?

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
 * @ODM\Document()
 */
class My_Doctrine_Model
{
    /** @ODM\id */
    protected $id;

    public function getId()
    {
        return $this->id;
    }

    public function setId($value)
    {
        $this->id = $value;
        return $this;
    }
}

And code

$myModel = new My_Doctrine_Model();
$myModel->setId(new MongoId());

// Id is my set id

$dm->persist($myModel);

// Id is my set id

$dm->flush();

// Id is overwritten and the object is inserted with an other one

Why is Doctrine overriding my dial id? And is there a way to prevent this?

The new identifier is set to PersistenceBuilder::prepareInsertDatawhen the check to check if the identifier is set says that it is not. I do not know why the id field is left outside the changeset.

Update

I have read a bit more code and found that the cause is the last ifin UnitOfWork::getDocumentActualData.

else if ( ! $class->isIdentifier($name) || $class->isIdGeneratorNone()) {
    $actualData[$name] = $value;
}

There is no value else, so id will not be set.
Is this an intentional design choice by developers?

solvable

, . . https://github.com/doctrine/mongodb-odm/commit/fb2447c01cb8968255383ac5700f95b4327468a3#L2L503

+3
1

, Doctrine ODM, -

/** Document */
class MyPersistentClass
{
    /** @Id(strategy="NONE") */
    private $id;

    public function setId($id)
    {
        $this->id = $id;
    }

    //...
}

Doctrine ODM - http://doctrine-mongodb-odm.readthedocs.org/en/latest/reference/basic-mapping.html#basic-mapping-identifiers

+1

All Articles