Add symfony expression syntax to collection relations annotation #6142

Closed
opened 2026-01-22 15:27:33 +01:00 by admin · 2 comments
Owner

Originally created by @rpodwika on GitHub (Dec 20, 2018).

Originally assigned to: @Ocramius on GitHub.

Feature Request

Q A
New Feature yes
RFC no
BC Break no

Summary

To use indexBy with foreign key you need to add the key as class property which is considered to be a bad practice.

Imagine situation when you have class Foo that hass class Related with relation type one to many

@ORM\OneToMany(targetEntity="Foo", mappedBy="foo", indexBy="relatedId")

I would like to use doctrine expression or be able to index by a foreign key.

@ORM\OneToMany(targetEntity="Foo", mappedBy="foo", indexBy="$related->getId()")

Originally created by @rpodwika on GitHub (Dec 20, 2018). Originally assigned to: @Ocramius on GitHub. ### Feature Request | Q | A |------------ | ------ | New Feature | yes | RFC | no | BC Break | no #### Summary To use indexBy with foreign key you need to add the key as class property which is considered to be a bad practice. Imagine situation when you have class Foo that hass class Related with relation type one to many `@ORM\OneToMany(targetEntity="Foo", mappedBy="foo", indexBy="relatedId")` I would like to use doctrine expression or be able to index by a foreign key. `@ORM\OneToMany(targetEntity="Foo", mappedBy="foo", indexBy="$related->getId()")`
admin added the New FeatureWon't FixQuestion labels 2026-01-22 15:27:33 +01:00
admin closed this issue 2026-01-22 15:27:33 +01:00
Author
Owner

@SenseException commented on GitHub (Dec 21, 2018):

Your current entity relation is: One Related entity can have one or more Foo entities and you want the collection of Foo use the id of Related? indexBy is supposed have a unique key.

indexBy is supposed to get a property with a unique value like in this example:

/**
 * @ORM\Entity
 */
class Foo
{
    /**
     * @var int
     *
     * @ORM\Column(type="integer")
     * @ORM\Id()
     * @ORM\GeneratedValue()
     */
    public $id;

    /**
     * @var string
     *
     * @ORM\Column(type="string")
     */
    public $name;

    /**
     * @var Bar[]
     *
     * Use Bar.id as key.
     * @ORM\OneToMany(targetEntity="Bar", mappedBy="foo", indexBy="id")
     */
    public $bars;
}
/**
 * @ORM\Entity
 */
class Bar
{
    /**
     * @var int
     *
     * @ORM\Column(type="integer")
     * @ORM\Id()
     * @ORM\GeneratedValue()
     */
    public $id;

    /**
     * @var string
     *
     * @ORM\Column(type="string")
     */
    public $name;

    /**
     * @var Foo
     *
     * @ORM\ManyToOne(targetEntity="Foo", inversedBy="bars")
     */
    public $foo;
}

Can you please elaborate your use case?

@SenseException commented on GitHub (Dec 21, 2018): Your current entity relation is: One `Related` entity can have one or more `Foo` entities and you want the collection of `Foo` use the id of `Related`? `indexBy` is supposed have a unique key. `indexBy` is supposed to get a property with a unique value like in this example: ``` php /** * @ORM\Entity */ class Foo { /** * @var int * * @ORM\Column(type="integer") * @ORM\Id() * @ORM\GeneratedValue() */ public $id; /** * @var string * * @ORM\Column(type="string") */ public $name; /** * @var Bar[] * * Use Bar.id as key. * @ORM\OneToMany(targetEntity="Bar", mappedBy="foo", indexBy="id") */ public $bars; } ``` ``` php /** * @ORM\Entity */ class Bar { /** * @var int * * @ORM\Column(type="integer") * @ORM\Id() * @ORM\GeneratedValue() */ public $id; /** * @var string * * @ORM\Column(type="string") */ public $name; /** * @var Foo * * @ORM\ManyToOne(targetEntity="Foo", inversedBy="bars") */ public $foo; } ``` Can you please elaborate your use case?
Author
Owner

@Ocramius commented on GitHub (Dec 21, 2018):

This:

indexBy="$related->getId()"

Is absolutely too complex, and an edge case too.

Closing as won't fix, since this logic is trivially implemented in userland with collection operations, without having to introduce massive complexity, pseudo-code in mappings, nor potential code parsing and evaluation facilities that are not worth building unless they are an evident game changer.

@Ocramius commented on GitHub (Dec 21, 2018): This: > `indexBy="$related->getId()"` Is absolutely too complex, and an edge case too. Closing as `won't fix`, since this logic is trivially implemented in userland with collection operations, without having to introduce massive complexity, pseudo-code in mappings, nor potential code parsing and evaluation facilities that are not worth building unless they are an evident game changer.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6142