[PR #6151] Embeddable attribute override support #9867

Open
opened 2026-01-22 16:05:40 +01:00 by admin · 0 comments
Owner

Original Pull Request: https://github.com/doctrine/orm/pull/6151

State: closed
Merged: No


This allows for overriding of embeddable mapping definitions from the parent entity/embeddable.

Nested embeddables are supported via dot notation along with the ability to drop column(s) from the embedded object if necessary.

The syntax is equivalent to attribute overrides on a subclass with the addition of passing a null value for a overridden column to drop it from the parent's field mappings.

Via annotations:

/** @Embeddable */
class ValueObject {

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

    /** @Column(type="integer") */
    public $count;

}

/** @Embeddable */
class NestedValueObject {

    /** @Embedded(class="ValueObject", columnPrefix=false) */
    public $nested;

}

/** @Entity */
class FooEntity {

    /** @Id @Column(type="integer") */
    private $id;

    /**
     * @Embedded(class="ValueObject", columnPrefix=false)
     * @AttributeOverride(name="value", column=@Column(type="string", name="single_value_override"))
     */
    public $valueObjectWithSingleOverride;

    /**
     * @Embedded(class="ValueObject", columnPrefix=false)
     * @AttributeOverrides({
     *     @AttributeOverride(name="value", column=@Column(type="string", name="multiple_value_override")),
     *     @AttributeOverride(name="count", column=null)
     * })
     */
    public $valueObjectWithMultipleOverrides;

    /**
     * @Embedded(class="NestedValueObject", columnPrefix=false)
     * @AttributeOverrides({
     *     @AttributeOverride(name="nested.value", column=@Column(type="string", name="nested_value_override")),
     *     @AttributeOverride(name="nested.count", column=null)
     * })
     */
    public $valueObjectWithNestedOverrides;
}

Via XML:

<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                            https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">
    <embeddable name="ValueObject">
        <field name="value" type="string" />
        <field name="count" type="integer" />
    </embeddable>
</doctrine-mapping>

<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                            https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">
    <embeddable name="NestedValueObject">
        <embedded name="nested"
                class="ValueObject"
                use-column-prefix="false"/>
    </embeddable>
</doctrine-mapping>

<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                            https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">
    <entity name="FooEntity" table="entity">
        <id name="id" column="id"/>
        <embedded name="valueObjectWithSingleOverride"
                class="ValueObject"
                use-column-prefix="false">
            <attribute-override name="value">
                <field column="single_value_override" type="string"/>
            </attribute-override>
        </embedded>
        <embedded name="valueObjectWithMultipleOverrides"
                class="ValueObject"
                use-column-prefix="false">
            <attribute-overrides>
                <attribute-override name="value">
                    <field column="multiple_value_override" type="string"/>
                </attribute-override>
                <attribute-override name="count"/>
            </attribute-overrides>
        </embedded>
        <embedded name="valueObjectWithNestedOverrides"
                class="NestedValueObject"
                use-column-prefix="false">
            <attribute-overrides>
                <attribute-override name="nested.value">
                    <field column="nested_value_override" type="string"/>
                </attribute-override>
                <attribute-override name="nested.count"/>
            </attribute-overrides>
        </embedded>
    </entity>
</doctrine-mapping>

Via YML:

ValueObject:
  type: embeddable
  fields:
    value:
      type: string
    count:
      type: integer

NestedValueObject:
  type: embeddable
  embedded:
    nested:
      class: ValueObject
      columnPrefix: false

FooEntity:
  type: entity
  id:
    id:
      type: integer
  embedded:
    valueObjectWithSingleOverride:
      class: ValueObject
      columnPrefix: false
      attributeOverrides:
        value:
          column: single_value_override
          type: string
    valueObjectWithMultipleOverrides:
      class: ValueObject
      columnPrefix: false
      attributeOverrides:
        value:
          column: multiple_value_override
          type: string
        count:
          column: null
    valueObjectWithNestedOverrides:
      class: NestedValueObject
      columnPrefix: false
      attributeOverrides:
        nested.value:
          column: nested_value_override
          type: string
        nested.count:
          column: null

A further improvement could be to merge the column override over the top of the original column instead of replacing it. Doing so would remove the need to redefine original column attributes even if they have not been changed. This change is outside the scope of this pull request.

References #6047

**Original Pull Request:** https://github.com/doctrine/orm/pull/6151 **State:** closed **Merged:** No --- This allows for overriding of embeddable mapping definitions from the parent entity/embeddable. Nested embeddables are supported via dot notation along with the ability to drop column(s) from the embedded object if necessary. The syntax is equivalent to attribute overrides on a subclass with the addition of passing a `null` value for a overridden column to drop it from the parent's field mappings. Via annotations: ```php /** @Embeddable */ class ValueObject { /** @Column(type="string") */ public $value; /** @Column(type="integer") */ public $count; } /** @Embeddable */ class NestedValueObject { /** @Embedded(class="ValueObject", columnPrefix=false) */ public $nested; } /** @Entity */ class FooEntity { /** @Id @Column(type="integer") */ private $id; /** * @Embedded(class="ValueObject", columnPrefix=false) * @AttributeOverride(name="value", column=@Column(type="string", name="single_value_override")) */ public $valueObjectWithSingleOverride; /** * @Embedded(class="ValueObject", columnPrefix=false) * @AttributeOverrides({ * @AttributeOverride(name="value", column=@Column(type="string", name="multiple_value_override")), * @AttributeOverride(name="count", column=null) * }) */ public $valueObjectWithMultipleOverrides; /** * @Embedded(class="NestedValueObject", columnPrefix=false) * @AttributeOverrides({ * @AttributeOverride(name="nested.value", column=@Column(type="string", name="nested_value_override")), * @AttributeOverride(name="nested.count", column=null) * }) */ public $valueObjectWithNestedOverrides; } ``` Via XML: ```xml <?xml version="1.0" encoding="UTF-8"?> <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd"> <embeddable name="ValueObject"> <field name="value" type="string" /> <field name="count" type="integer" /> </embeddable> </doctrine-mapping> <?xml version="1.0" encoding="UTF-8"?> <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd"> <embeddable name="NestedValueObject"> <embedded name="nested" class="ValueObject" use-column-prefix="false"/> </embeddable> </doctrine-mapping> <?xml version="1.0" encoding="UTF-8"?> <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd"> <entity name="FooEntity" table="entity"> <id name="id" column="id"/> <embedded name="valueObjectWithSingleOverride" class="ValueObject" use-column-prefix="false"> <attribute-override name="value"> <field column="single_value_override" type="string"/> </attribute-override> </embedded> <embedded name="valueObjectWithMultipleOverrides" class="ValueObject" use-column-prefix="false"> <attribute-overrides> <attribute-override name="value"> <field column="multiple_value_override" type="string"/> </attribute-override> <attribute-override name="count"/> </attribute-overrides> </embedded> <embedded name="valueObjectWithNestedOverrides" class="NestedValueObject" use-column-prefix="false"> <attribute-overrides> <attribute-override name="nested.value"> <field column="nested_value_override" type="string"/> </attribute-override> <attribute-override name="nested.count"/> </attribute-overrides> </embedded> </entity> </doctrine-mapping> ``` Via YML: ```yaml ValueObject: type: embeddable fields: value: type: string count: type: integer NestedValueObject: type: embeddable embedded: nested: class: ValueObject columnPrefix: false FooEntity: type: entity id: id: type: integer embedded: valueObjectWithSingleOverride: class: ValueObject columnPrefix: false attributeOverrides: value: column: single_value_override type: string valueObjectWithMultipleOverrides: class: ValueObject columnPrefix: false attributeOverrides: value: column: multiple_value_override type: string count: column: null valueObjectWithNestedOverrides: class: NestedValueObject columnPrefix: false attributeOverrides: nested.value: column: nested_value_override type: string nested.count: column: null ``` A further improvement could be to merge the column override over the top of the original column instead of replacing it. Doing so would remove the need to redefine original column attributes even if they have not been changed. This change is outside the scope of this pull request. References #6047
admin added the pull-request label 2026-01-22 16:05:40 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#9867