DDC-3334: Allow to set @Id in @AttributeOverride #4119

Open
opened 2026-01-22 14:35:31 +01:00 by admin · 4 comments
Owner

Originally created by @doctrinebot on GitHub (Oct 2, 2014).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user j_schumann:

Hello!

@AttributeOverride only allows to change the column definition of a property that is defined in a parent class or trait. It is not possible to define such an "foreign" column (through inheritance/trait) as ID column and also there is no possibility define the primary key through any other annotation.

It would be greatly useful to extend the @AttributeOverride to allow specification of @ID, @GeneratedValue and @CustomIdGenerator in this annotation as those are also property/attribute related and there is no reason those should not be overrideable.

Example:

trait ForeignColumn
{
    /****
     * @ORM\Column(type="string", nullable=true)
     */
    protected $foreignColum = null;
}

/****
 * @ORM\Entity()
 * @ORM\AttributeOverrides({
 *      @ORM\AttributeOverride(name="foreignColum",
 *          column=@ORM\Column(type="string", nullable=false),
 *      )
 * })
 */
class MyEntity {
  use ForeignColum;

    /****
     * @ORM\Id
     * @ORM\Column(type="string", nullable=false)
     */
    protected $myColum = null;
}```

In this example it is not possible to add the property to the class again and annotate it directly as this would throw a Strict Error as trait properties cannot be overwritten.

It is also not possible to create a composite key with foreignColumn and myColumn (and it is also not be possible to define only foreignColumn as primary key without modifying the trait).
Originally created by @doctrinebot on GitHub (Oct 2, 2014). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user j_schumann: Hello! @AttributeOverride only allows to change the column definition of a property that is defined in a parent class or trait. It is not possible to define such an "foreign" column (through inheritance/trait) as ID column and also there is no possibility define the primary key through any other annotation. It would be greatly useful to extend the @AttributeOverride to allow specification of @ID, @GeneratedValue and @CustomIdGenerator in this annotation as those are also property/attribute related and there is no reason those should not be overrideable. Example: `````` trait ForeignColumn { /**** * @ORM\Column(type="string", nullable=true) */ protected $foreignColum = null; } /**** * @ORM\Entity() * @ORM\AttributeOverrides({ * @ORM\AttributeOverride(name="foreignColum", * column=@ORM\Column(type="string", nullable=false), * ) * }) */ class MyEntity { use ForeignColum; /**** * @ORM\Id * @ORM\Column(type="string", nullable=false) */ protected $myColum = null; }``` In this example it is not possible to add the property to the class again and annotate it directly as this would throw a Strict Error as trait properties cannot be overwritten. It is also not possible to create a composite key with foreignColumn and myColumn (and it is also not be possible to define only foreignColumn as primary key without modifying the trait). ``````
admin added the Improvement label 2026-01-22 14:35:31 +01:00
Author
Owner

@taylankasap commented on GitHub (Jul 20, 2016):

I expect the usage should be like this:

/**
 * @ORM\Entity
 * @ORM\AttributeOverrides({
 *      @ORM\AttributeOverride(name="foreignColumn",
 *          column=@ORM\Id
 *      )
 * })
 */

But I don't think this should be limited to certain annotations (e.g. @ORM\Id). Adding custom annotations should be possible. For example I need to add @Gedmo\SortableGroup and I thought this might work:

/**
 * @ORM\Entity
 * @ORM\AttributeOverrides({
 *      @ORM\AttributeOverride(name="tenant",
 *          column=@Gedmo\SortableGroup
 *      )
 * })
 */

But the error message is (same with error message for @ORM\Id)

[Doctrine\Common\Annotations\AnnotationException]
[Semantical Error] Annotation @Gedmo\SortableGroup is not allowed to be declared on class AppBundle\Entity\Tenant\Person. You may only use this annotation on these code elements: PROPERTY.

Apparently DocParser thinks I'm trying to use this annotation on the class itself instead of understanding it is an override.

Also if this is going to become a thing than Doctrine\ORM\Mapping\AttributeOverride::$column looks wrong if it's name implies only @ORM\Column is overridable. If that's the case something like Doctrine\ORM\Mapping\AttributeOverride::$annotation might be better.

@taylankasap commented on GitHub (Jul 20, 2016): I expect the usage should be like this: ``` /** * @ORM\Entity * @ORM\AttributeOverrides({ * @ORM\AttributeOverride(name="foreignColumn", * column=@ORM\Id * ) * }) */ ``` But I don't think this should be limited to certain annotations (e.g. `@ORM\Id`). Adding custom annotations should be possible. For example I need to add `@Gedmo\SortableGroup` and I thought this might work: ``` /** * @ORM\Entity * @ORM\AttributeOverrides({ * @ORM\AttributeOverride(name="tenant", * column=@Gedmo\SortableGroup * ) * }) */ ``` But the error message is (same with error message for `@ORM\Id`) ``` [Doctrine\Common\Annotations\AnnotationException] [Semantical Error] Annotation @Gedmo\SortableGroup is not allowed to be declared on class AppBundle\Entity\Tenant\Person. You may only use this annotation on these code elements: PROPERTY. ``` Apparently DocParser thinks I'm trying to use this annotation on the class itself instead of understanding it is an override. Also if this is going to become a thing than `Doctrine\ORM\Mapping\AttributeOverride::$column` looks wrong if it's name implies only `@ORM\Column` is overridable. If that's the case something like `Doctrine\ORM\Mapping\AttributeOverride::$annotation` might be better.
Author
Owner

@taylankasap commented on GitHub (Jul 20, 2016):

Also if you make it like this, I think @ORM\AssociationOverrides will no longer be needed. If I'm not missing something, that is.

@taylankasap commented on GitHub (Jul 20, 2016): Also if you make it like this, I think `@ORM\AssociationOverrides` will no longer be needed. If I'm not missing something, that is.
Author
Owner

@MatTheCat commented on GitHub (Jun 20, 2017):

I just stumbled upon this, could this be implemented someday?

@MatTheCat commented on GitHub (Jun 20, 2017): I just stumbled upon this, could this be implemented someday?
Author
Owner

@ipesanz commented on GitHub (Aug 26, 2017):

I was searching to have different strategies of Generating ID depending on which subclas of User amb I declaring, for example:
Abstract User @Entity Class, @inheritanceType = SingleTable --> estrategy Auto (althought i cant declare this type)

  • AdminUser --> Increment by 1000
  • ReaderUser --> Increment by 1

Of course I have to take care of this way, but I was dealing which way to do and I was not able to find anything related so.... I change my mind

@ipesanz commented on GitHub (Aug 26, 2017): I was searching to have different strategies of Generating ID depending on which subclas of User amb I declaring, for example: Abstract User @Entity Class, @inheritanceType = SingleTable --> estrategy Auto (althought i cant declare this type) - AdminUser --> Increment by 1000 - ReaderUser --> Increment by 1 Of course I have to take care of this way, but I was dealing which way to do and I was not able to find anything related so.... I change my mind
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#4119