Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-250

ArrayCollection Key Column @indexBy

    Details

    • Type: New Feature New Feature
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.1
    • Component/s: Mapping Drivers
    • Security Level: All
    • Labels:
      None

      Description

      To be honest I feel like I saw this mentioned somewhere, but after looking around I couldn't find it, so I'll just post this anyway.

      I'd like the ability to set a property as the associative key on ArrayCollections. This functionality could probably be done in a PostLoad by iterating over the objects, but it seems like ArrayCollection should be able to handle this functionality already, it's just a matter of modifying the mapper/parser.

      Example:

      /**
       * @Entity
       */
      class Object
      {
      //... $id column and anything else ...
          /**
           * @OneToMany(targetEntity="Param", mappedBy="object")
           */
          protected $params;
      //...
          public function getParam($name)
          {
               return $this->params[$name];
          }
      }
      
      /**
       * @Entity
       */
      class Param
      {
          /**
           * @ManyToOne(targetEntity="Object")
           * @JoinColumn(name="object_id", referencedColumnName="id", nullable="false")
           */
          protected $object;
          /**
           * @Column(type="string")
           */
          protected $name;
          /**
           * @Column(type="string")
           */
          protected $value;
      }
      

      If you were able to specify an ArrayCollection key column then you would be able to have functions like getParam without having to loop over the objects in the collection. Something like:

       @OneToMany(targetEntity="Param", mappedBy="object", collectionKey="name") 

        Issue Links

          Activity

          Hide
          Benjamin Eberlei added a comment - - edited

          This is now in master https://github.com/doctrine/doctrine2/commit/7390030854cdb4b775f6f11f83c970ab2705e924

          Syntax is:

          @ManyToMany(targetEntity="OtherEntity", indexBy="foreignField")
          @OneToMany(targetEntity="OtherEntity", indexBy="foreignField")
          
          <many-to-many index-by="foreignField" />
          
          manyToMany:
            indexBy: foreignField
          
          Show
          Benjamin Eberlei added a comment - - edited This is now in master https://github.com/doctrine/doctrine2/commit/7390030854cdb4b775f6f11f83c970ab2705e924 Syntax is: @ManyToMany(targetEntity= "OtherEntity" , indexBy= "foreignField" ) @OneToMany(targetEntity= "OtherEntity" , indexBy= "foreignField" ) <many-to-many index-by= "foreignField" /> manyToMany: indexBy: foreignField
          Hide
          Benjamin Eberlei added a comment -

          A related Github Pull-Request [GH-470] was opened
          https://github.com/doctrine/doctrine2/pull/470

          Show
          Benjamin Eberlei added a comment - A related Github Pull-Request [GH-470] was opened https://github.com/doctrine/doctrine2/pull/470
          Hide
          Doctrine Bot added a comment -

          A related Github Pull-Request [GH-470] was closed:
          https://github.com/doctrine/dbal/pull/470

          Show
          Doctrine Bot added a comment - A related Github Pull-Request [GH-470] was closed: https://github.com/doctrine/dbal/pull/470
          Hide
          Doctrine Bot added a comment -

          A related Github Pull-Request [GH-470] was assigned:
          https://github.com/doctrine/doctrine2/pull/470

          Show
          Doctrine Bot added a comment - A related Github Pull-Request [GH-470] was assigned: https://github.com/doctrine/doctrine2/pull/470
          Hide
          Doctrine Bot added a comment -

          A related Github Pull-Request [GH-470] was closed:
          https://github.com/doctrine/doctrine2/pull/470

          Show
          Doctrine Bot added a comment - A related Github Pull-Request [GH-470] was closed: https://github.com/doctrine/doctrine2/pull/470

            People

            • Assignee:
              Benjamin Eberlei
              Reporter:
              Michael Ridgway
            • Votes:
              1 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: