Skip to content

Loading…

DDC-250: ArrayCollection Key Column @indexBy #3221

Closed
doctrinebot opened this Issue · 12 comments

2 participants

@doctrinebot

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")

@doctrinebot

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

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

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

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

Comment created by @beberlei:

Schedule for 2.1

@doctrinebot

Comment created by @beberlei:

This is now in master 7390030

Syntax is:

@ManyToMany(targetEntity="OtherEntity", indexBy="foreignField")
@OneToMany(targetEntity="OtherEntity", indexBy="foreignField")
<many-to-many index-by="foreignField" />
manyToMany:
  indexBy: foreignField
@doctrinebot

Issue was closed with resolution "Fixed"

@doctrinebot

Comment created by @beberlei:

A related Github Pull-Request [GH-470] was opened
#470

@doctrinebot

Comment created by @doctrinebot:

A related Github Pull-Request [GH-470] was closed:
doctrine/dbal#470

@doctrinebot

Comment created by @doctrinebot:

A related Github Pull-Request [GH-470] was assigned:
#470

@doctrinebot

Comment created by @doctrinebot:

A related Github Pull-Request [GH-470] was closed:
#470

@beberlei beberlei was assigned by doctrinebot
@doctrinebot doctrinebot added this to the 2.1 milestone
@doctrinebot doctrinebot closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.