Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-2414

Unable to create One-To-Many relation with composite keys

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Invalid
    • Affects Version/s: Git Master, 2.3.3
    • Fix Version/s: None
    • Component/s: Mapping Drivers, Tools
    • Security Level: All
    • Labels:
    • Environment:
      Ubuntu 12.04, PHP 5.4.9

      Description

      Given these entities :

      /**
       * Class Domain
       *
       * @Entity
       * @Table(name="profils_domains")
       */
      class Domain
      {
          /**
           * @var string
           *
           * @Id
           * @Column(type="string", length=22, nullable=false)
           */
          protected $name = '';
      }
      
      /**
       * Class User
       *
       * @Entity
       * @Table(name="profils_users")
       */
      class User
      {
          /**
           * @var string
           *
           * @Id
           * @Column(type="string", length=22, nullable=false)
           */
          protected $name = '';
      
          /**
           * @var Domain
           *
           * @Id
           * @ManyToOne(targetEntity="Domain", fetch="LAZY")
           * @JoinColumn(name="domain", referencedColumnName="name", onDelete="CASCADE")
           */
          protected $domain;
          
          /**
           * @var Group[]|ArrayCollection
           *
           * @ManyToMany(targetEntity="Group", mappedBy="users")
           */
          protected $groups;
      }
      
      /**
       * Class Group
       *
       * @Entity
       * @Table(name="profils_groups")
       */
      class Group
      {
          /**
           * @var string
           *
           * @Id
           * @Column(type="string", length=22, nullable=false)
           */
          protected $name = '';
      
          /**
           * @var Domain
           *
           * @Id
           * @ManyToOne(targetEntity="Domain", fetch="LAZY")
           * @JoinColumn(name="domain", referencedColumnName="name", onDelete="CASCADE")
           */
          protected $domain;
          
          /**
           * @var User[]|ArrayCollection
           *
           * @ManyToMany(targetEntity="User", indexBy="name", fetch="EXTRA_LAZY")
           * @JoinTable(name="profils_groups_users",
           *      joinColumns={
           * @JoinColumn(name="group_name", referencedColumnName="name", onDelete="CASCADE"),
           * @JoinColumn(name="domain", referencedColumnName="domain", onDelete="CASCADE")
           *          },
           *      inverseJoinColumns={
           * @JoinColumn(name="user_name", referencedColumnName="name", onDelete="CASCADE"),
           * @JoinColumn(name="domain", referencedColumnName="domain", onDelete="CASCADE")
           *          }
           *      )
           */
          protected $users;
      }
      

      I want to link users and groups but only from the same domain.
      I also want a user to be in one group only.
      The only way with composite keys is to make One-To-Many, Unidirectional with Join Table but I can't put unique=true in the @JoinColumn of my inverseJoinColumns because it will generate a unique index for each field and not one composite. I also can't use @UniqueConstraint as it is not supported in @JoinTable.

        Activity

        Bruno CHALOPIN created issue -
        Benjamin Eberlei made changes -
        Field Original Value New Value
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Invalid [ 6 ]

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Bruno CHALOPIN
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: