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 the 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 does Doctrine override my set id? And is there a way I can prevent this?
The new Id is set in PersistenceBuilder::prepareInsertData
when the check to see if an id is set says it isn't. I don't know why the id field is left out of the change set.
Update
I read a little more code and found that the reason is the last if
in UnitOfWork::getDocumentActualData
.
else if ( ! $class->isIdentifier($name) || $class->isIdGeneratorNone()) {
$actualData[$name] = $value;
}
There is no else
so no value get set for the id.
Is this a deliberate design choice by the developers?
Solved
This was updated in a recent commit which I haven't updated to. I am not completely sure this is intended though.
https://github.com/doctrine/mongodb-odm/commit/fb2447c01cb8968255383ac5700f95b4327468a3#L2L503