本文介绍了Symfony,ODM:如何设置乘法ID注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以问题是如何提供两个或多个标识符键?我在Google搜索中找不到关于此问题的任何答案。例如:

So the question is how to provide two or more identifiers keys? I couldn't find any answers on this question in google search... Here is example:

class Customer
{
    /**
     * @ODM\Id
     *
     * @JMS\Expose
     * @JMS\Type("string")
     *
     */
    protected $id;

    /**
     * @var integer
     * @ODM\Id(strategy="INCREMENT")
     *
     * @JMS\Expose
     * @JMS\Type("integer")
     *
     */
    protected $customerId;

因此,在这种情况下,我有第二个id,随着我的写作而增加,但第一个id为null。如果我删除并只写 * @ ODM\Field(type = integer),一切都很好,但是 customerId 。那么,我该如何在文档中添加ID?还是我错了,我不这样做?

So in this case I have second id which increment as I wrote, but first id became null. If I remove and write just * @ODM\Field(type="integer") everything is ok, but no increment of customerId. So how can I have to ids in document? Or I'm wrong and I don't do this?

推荐答案

标识符自动映射为 _id 字段,因此只能有1个字段映射为 @Id

Identifier is automatically mapped as _id field therefore there can be only 1 field mapped as @Id.

过去做过类似的事情,我建议保留 \MongoId 作为文档标识符并生成递增的 customerId ,而不是依靠ODM这样做。制作这样的生成器非常简单,您需要插入持久性文档(我建议在域代码中使用它,或者利用 prePersist 事件)并编写类似于ODM的生成器 IncrementGenerator

Done similar things in the past and I'd suggest keeping \MongoId as document identifier and generating incremented customerId in your code instead of relying on ODM to do so. Making such generator is pretty straightforward, you need to hook into persisting document (be it in your domain code, which I advice, or leveraging prePersist event) and write generator similar to ODM's IncrementGenerator.

这篇关于Symfony,ODM:如何设置乘法ID注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 20:46