Override Field Association Mappings In Subclasses

Sometimes there is a need to persist entities but override all or part of the mapping metadata. Sometimes also the mapping to override comes from entities using traits where the traits have mapping metadata. This tutorial explains how to override mapping metadata, i.e. attributes and associations metadata in particular. The example here shows the overriding of a class that uses a trait but is similar when extending a base class as shown at the end of this tutorial.

Suppose we have a class ExampleEntityWithOverride. This class uses trait ExampleTrait:

1<?php #[Entity] #[AttributeOverrides([ new AttributeOverride('foo', new Column( name: 'foo_overridden', type: 'integer', length: 140, nullable: false, unique: false, )), ])] #[AssociationOverrides([ new AssociationOverride('bar', [ new JoinColumn( name: 'example_entity_overridden_bar_id', referencedColumnName: 'id', ), ]), ])] class ExampleEntityWithOverride { use ExampleTrait; } #[Entity] class Bar { #[Id, Column(type: 'string')] private $id; }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

#[AttributeOverrides] contains metadata override of the attribute and association type. It basically changes the names of the columns mapped for a property foo and for the association bar which relates to Bar class shown above. Here is the trait which has mapping metadata that is overridden by the attribute above:

1<?php /** * Trait class */ trait ExampleTrait { #[Id, Column(type: 'integer')] private int|null $id = null; #[Column(name: 'trait_foo', type: 'integer', length: 100, nullable: true, unique: true)] protected int $foo; #[OneToOne(targetEntity: Bar::class, cascade: ['persist'])] #[JoinColumn(name: 'example_trait_bar_id', referencedColumnName: 'id')] protected Bar|null $bar = null; }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

The case for just extending a class would be just the same but:

1<?php class ExampleEntityWithOverride extends BaseEntityWithSomeMapping { // ... }
2
3
4
5

Overriding is also supported via XML (examples).