DDC-250: ArrayCollection Key Column @indexBy #308

Closed
opened 2026-01-22 12:34:05 +01:00 by admin · 12 comments
Owner

Originally created by @doctrinebot on GitHub (Jan 12, 2010).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user mridgway:

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:
none @OneToMany(targetEntity="Param", mappedBy="object", collectionKey="name")

Originally created by @doctrinebot on GitHub (Jan 12, 2010). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user mridgway: 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: ``` none /**** * @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: `none @OneToMany(targetEntity="Param", mappedBy="object", collectionKey="name")`
admin added the New Feature label 2026-01-22 12:34:05 +01:00
admin closed this issue 2026-01-22 12:34:06 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jan 12, 2010):

@doctrinebot commented on GitHub (Jan 12, 2010): - depends on [DDC-1018: INDEX BY does not work in JOIN clauses](http://www.doctrine-project.org/jira/browse/DDC-1018) - relates to [DDC-213: Persist order of collections](http://www.doctrine-project.org/jira/browse/DDC-213)
Author
Owner

@doctrinebot commented on GitHub (Apr 19, 2010):

Comment created by marijn:

As much as I would like this I doubt it is -possible- feasible. Imagine the name column of your Param object not being unique for you your linked Object..? How could the ArrayCollection know which instance to add to the collection and which one not?

@doctrinebot commented on GitHub (Apr 19, 2010): Comment created by marijn: As much as I would like this I doubt it is -possible- feasible. Imagine the `name` column of your `Param` object not being unique for you your linked `Object`..? How could the `ArrayCollection` _know_ which instance to add to the collection and which one not?
Author
Owner

@doctrinebot commented on GitHub (Apr 19, 2010):

Comment created by mridgway:

You are absolutely correct. There is no way to ensure the uniqueness of the collection elements. This is something that would have to be ensured in the domain model and shouldn't have anything to do with Doctrine.

It will certainly work to loop through the collection on PostLoad (once DDC-54 is implemented). For now, I just set the keys the first time I call getParam().

I suppose I should close this (I forgot it was still open).

@doctrinebot commented on GitHub (Apr 19, 2010): Comment created by mridgway: You are absolutely correct. There is no way to ensure the uniqueness of the collection elements. This is something that would have to be ensured in the domain model and shouldn't have anything to do with Doctrine. It will certainly work to loop through the collection on PostLoad (once [DDC-54](http://www.doctrine-project.org/jira/browse/DDC-54) is implemented). For now, I just set the keys the first time I call getParam(). I suppose I should close this (I forgot it was still open).
Author
Owner

@doctrinebot commented on GitHub (Oct 6, 2010):

Comment created by mridgway:

I just noticed that DQL has an INDEX BY keyword to be able to do this during a DQL query. Is this something that could then be possible to set in a oneToMany or manyToMany? It seems like most of the groundwork for being able to do this is already done.

@doctrinebot commented on GitHub (Oct 6, 2010): Comment created by mridgway: I just noticed that DQL has an INDEX BY keyword to be able to do this during a DQL query. Is this something that could then be possible to set in a oneToMany or manyToMany? It seems like most of the groundwork for being able to do this is already done.
Author
Owner

@doctrinebot commented on GitHub (Dec 24, 2010):

Comment created by @beberlei:

Example code:

<?php

class Article
{
    /*** @Id @Column(type="integer") @GeneratedValue **/
    public $id;

    /**** @Column(type="string")
    public $headline;

    /****
     * @OneToMany(targetEntity="Translation", mappedBy="article")
     * @IndexBy("language")
     */
    public $translations;
}

class Translation
{
    /****
     * @Id @Column(type="string")
     */
    public $language;

    /****
     * @Id @ManyToOne(targetEntity="Article", inversedBy="translations")
     */
    public $article;

    /****
     * @Column(type="string")
     */
    public $headline;
}


$article = $em->find("Article", 1);
$article->translations["en"]->headlinle;

In this case @IndexBy uses the Translation::$language as key for the collection.

@doctrinebot commented on GitHub (Dec 24, 2010): Comment created by @beberlei: Example code: ``` <?php class Article { /*** @Id @Column(type="integer") @GeneratedValue **/ public $id; /**** @Column(type="string") public $headline; /**** * @OneToMany(targetEntity="Translation", mappedBy="article") * @IndexBy("language") */ public $translations; } class Translation { /**** * @Id @Column(type="string") */ public $language; /**** * @Id @ManyToOne(targetEntity="Article", inversedBy="translations") */ public $article; /**** * @Column(type="string") */ public $headline; } $article = $em->find("Article", 1); $article->translations["en"]->headlinle; ``` In this case @IndexBy uses the Translation::$language as key for the collection.
Author
Owner

@doctrinebot commented on GitHub (Dec 24, 2010):

Comment created by @beberlei:

Schedule for 2.1

@doctrinebot commented on GitHub (Dec 24, 2010): Comment created by @beberlei: Schedule for 2.1
Author
Owner

@doctrinebot commented on GitHub (Feb 5, 2011):

Comment created by @beberlei:

This is now in master 7390030854

Syntax is:

@ManyToMany(targetEntity="OtherEntity", indexBy="foreignField")
@OneToMany(targetEntity="OtherEntity", indexBy="foreignField")
<many-to-many index-by="foreignField" />
manyToMany:
  indexBy: foreignField
@doctrinebot commented on GitHub (Feb 5, 2011): Comment created by @beberlei: 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 ```
Author
Owner

@doctrinebot commented on GitHub (Feb 5, 2011):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Feb 5, 2011): Issue was closed with resolution "Fixed"
Author
Owner

@doctrinebot commented on GitHub (Oct 11, 2012):

Comment created by @beberlei:

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

@doctrinebot commented on GitHub (Oct 11, 2012): Comment created by @beberlei: A related Github Pull-Request [GH-470] was opened https://github.com/doctrine/doctrine2/pull/470
Author
Owner

@doctrinebot commented on GitHub (Dec 29, 2013):

Comment created by @doctrinebot:

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

@doctrinebot commented on GitHub (Dec 29, 2013): Comment created by @doctrinebot: A related Github Pull-Request [GH-470] was closed: https://github.com/doctrine/dbal/pull/470
Author
Owner

@doctrinebot commented on GitHub (Jan 12, 2015):

Comment created by @doctrinebot:

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

@doctrinebot commented on GitHub (Jan 12, 2015): Comment created by @doctrinebot: A related Github Pull-Request [GH-470] was assigned: https://github.com/doctrine/doctrine2/pull/470
Author
Owner

@doctrinebot commented on GitHub (Jan 13, 2015):

Comment created by @doctrinebot:

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

@doctrinebot commented on GitHub (Jan 13, 2015): Comment created by @doctrinebot: A related Github Pull-Request [GH-470] was closed: https://github.com/doctrine/doctrine2/pull/470
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#308