0

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;

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?

2 Answers2

1

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

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.

malarzm
  • 2,831
  • 2
  • 15
  • 25
0

You can only have two key one of string the other integer as part of a composite key, as per this documentation: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/composite-primary-keys.html

So try:

class Customer
{
    /**
     * @ODM\Id
     * @ORM\Column(name="id", type="string")
     *
     * @JMS\Expose
     * @JMS\Type("string")
     *
     */
    protected $id;

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

I think that should work for you.

Alvin Bunk
  • 7,621
  • 3
  • 29
  • 45
  • Be attentive! I do not use ODM, so the ORM notation have no sense, and solutions for ORM cant implement to ODM notation. I use only ODM, only mongodb. – Nikita_kharkov_ua Feb 14 '17 at 07:16
  • 1
    Sorry, your post had the example with `ORM` so I was mistaken. I did a search online and I believe Doctrine with ODM does not support composite Ids. However, I found this link: http://stackoverflow.com/questions/7222062/mongodb-composite-key/7225657#7225657 which might be helpful for you. I can remove my answer if you think it's unsuitable and post what's in that link as an answer. – Alvin Bunk Feb 14 '17 at 16:09