English 中文(简体)
我怎么能够把我自己的MongoId与MongoDB的Doctrine ODM混在一起?
原标题:How can I set my own MongoId with Doctrine ODM for MongoDB?
use DoctrineODMMongoDBMappingAnnotations as ODM;

/**
 * @ODMDocument()
 */
class My_Doctrine_Model
{
    /** @ODMid */
    protected $id;

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

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

法典

$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

为什么教条凌驾于我的 set? 我能否防止这种情况?

新 页: 1 PersistenceBuilder:prepareInsertData 当检查看是否有id时,就没有。 我不知道,为什么这个荒谬的田地脱离了变化。

Update

我读了一部略微多的法典,发现其原因是最后的<编码>if。 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

问题回答

您可使用一种习俗识别战略,使用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/fer/basic-mapping.html#basic-mapping-identifiers/





相关问题
Doctrine quoting apostrophes

I am using Doctrine ORM for php and Zend MVC. I have a mysql database. When I insert into the database it is escaping quotes. In other-words when I enter <input name="customer_name" value="Test ...

What does mutators and accessors do in Doctrine?

In this document, it explains how to use mutators and accessors in Doctrine, but does not explain what they are. Could anyone explain what mutators and accessors do and what they are? Thanks in ...

Tracking the views of a given row

I have a site where the users can view quite a large number of posts. Every time this is done I run a query similar to UPDATE table SET views=views+1 WHERE id = ?. However, there are a number of ...

Doctrine schema.yml generator

I am pretty new to doctrine. I made two small projects with doctrine for my own but now I am about to create big project for my client. The project will have more than 50 tables. Is there any way of ...

How does Doctrine handle changes to the database schema?

In brief, what happens when you add a column to a table? What happens when you remove one? In more details, suppose you have the following: class User extends Doctrine_Record { public function ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签