Doctrine composite primary key

16,838

It was right there in the documentation. The answer to my question was by using the associationKey.

http://doctrine-orm.readthedocs.org/en/stable/tutorials/composite-primary-keys.html

If you go down to Dynamic Attributes on the link above, the solution lies there. The limitation however being, I wanted one field of the composite primary key to be auto generated, which I understand is not supported by Doctrine 2.1.

Share:
16,838
softie
Author by

softie

Learning software developer

Updated on June 09, 2022

Comments

  • softie
    softie almost 2 years

    I know Doctrine 2.0 supports composite keys. But I am not quite able to find out what I am looking for.

    I have 3 tables => agent, client and client_group.
    Agent creates client and client_group. Client_Group has a composite primary key => id, agent_id. agent_id comes from the agent table. While I want the id to be auto incremented, the agent_id comes from agent table.
    I am trying to write the yaml files but I am not sure how to specify this agent_id as a primary key in client_group. client_group_id and agent_id are foreign keys in client table. Also, I did read on the Doctrine documentation that composite PK cannot have fields that auto increment.

    So I am wondring if I can really do what I am aiming to produce here.

    The yaml showing only the relations look like this:

    agent:

      oneToMany:
        client_group:
          targetEntity: ClientGroup
          mappedBy: agent
        client:
          targetEntity: Client
          mappedBy: agent
    

    client_group

      manyToOne:
        agent_id:
          targetEntity: Agent
          inversedBy: client_group
          joinColumn:
            name: agent_id
            referencedColumnName: id
    
      oneToMany:
        client:
          targetEntity: Client
          mappedBy: client_group
    

    client

      manyToOne:
        client:
          targetEntity: ClientGroup
          inversedBy: client
          joinColumn:
            name: client_group_id
            referencedColumnName: id
        agent:
          targetEntity: Agent
          inversedBy: agents
          joinColumn:
            name: agent_id
            referencedColumnName: id
    

    Any help on this would be aprreciated. Thanks.

  • Wilt
    Wilt over 10 years
    "I wanted one field of the composite primary key to be auto generated, which I understand is not supported by Doctrine 2.1", that is exactly whay I wanted too... It seems a normal thing to do. For example a user has phoneNumbers, one part of the key is the user_id the other is an auto generated numeric Id value.
  • Bruno
    Bruno over 7 years