Skip to content

Commit 6d306c1

Browse files
authored
Support for nesting attributes with PHP 8.1 (#9241)
* [GH-9240] Refactor Association/AttributeOverrides to use @NamedConstructorArguments with AnnotationDriver. * [GH-9240] Add support for PHP 8.1 nested attributes. Supported/new attributes are #[AttributeOverrides], #[AssociationOverrides], #[JoinTable] with nested joinColumns, inverseJoinColumns. * [GH-9240] Add support for nesting Index, UniqueCosntraint into #[Table] on PHP 8.1 * Apply review comments by gregooire. * Add documentation for new attributes. * Add docs for new nested #[JoinTable] support of join columns * Add docs for new nested #[Table] support of index, uniqueConstraints * Rename "Required/Optional atttributes" to "Required/Optional parameters" * Remove nesting for JoinTable#joinColumns and Table#indexes/uniqueConstraints again. * Hosuekeeping: phpcs/psalm * housekeeping * Remove unused function imports.
1 parent bea5e71 commit 6d306c1

22 files changed

+391
-76
lines changed

docs/en/reference/attributes-reference.rst

Lines changed: 119 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ annotation metadata supported since the first version 2.0.
1010
Index
1111
-----
1212

13+
- :ref:`#[AssociationOverride] <attrref_associationoverride]`
14+
- :ref:`#[AttributeOverride] <attrref_attributeoverride]`
1315
- :ref:`#[Column] <attrref_column>`
1416
- :ref:`#[Cache] <attrref_cache>`
1517
- :ref:`#[ChangeTrackingPolicy <attrref_changetrackingpolicy>`
@@ -49,6 +51,93 @@ Index
4951
Reference
5052
---------
5153

54+
.. _attrref_associationoverride:
55+
56+
#[AssociationOverride]
57+
~~~~~~~~~~~~~~~~~~~~~~
58+
59+
In an inheritance hierarchy this attribute allows to override the
60+
assocation mapping definitions of the parent mappings. It needs to be nested
61+
within a ``#[AssociationOverrides]`` on the class level.
62+
63+
Required parameters:
64+
65+
- **name**: Name of the association mapping to overwrite.
66+
67+
Optional parameters:
68+
69+
- **joinColumns**: A list of nested ``#[JoinColumn]`` declarations.
70+
- **joinTable**: A nested ``#[JoinTable]`` declaration in case of a many-to-many overwrite.
71+
- **inversedBy**: The name of the inversedBy field on the target entity side.
72+
- **fetch**: The fetch strategy, one of: EAGER, LAZY, EXTRA_LAZY.
73+
74+
Examples:
75+
76+
.. code-block:: php
77+
78+
<?php
79+
use Doctrine\ORM\Mapping\AssociationOverride;
80+
use Doctrine\ORM\Mapping\AssociationOverrides;
81+
use Doctrine\ORM\Mapping\Column;
82+
use Doctrine\ORM\Mapping\Entity;
83+
84+
#[AssociationOverrides([
85+
new AssociationOverride(
86+
name: "groups",
87+
joinTable: new JoinTable(
88+
name: "ddc964_users_admingroups",
89+
),
90+
joinColumns: [new JoinColumn(name: "adminuser_id")],
91+
inverseJoinColumns: [new JoinColumn(name: "admingroup_id")]
92+
),
93+
new AssociationOverride(
94+
name: "address",
95+
joinColumns: [new JoinColumn(name: "adminaddress_id", referencedColumnName: "id")]
96+
)
97+
])]
98+
class DDC964Admin extends DDC964User
99+
{
100+
}
101+
102+
.. _attrref_attributeoverride:
103+
104+
#[AttributeOverride]
105+
~~~~~~~~~~~~~~~~~~~~
106+
107+
In an inheritance hierarchy this attribute allows to override the
108+
field mapping definitions of the parent mappings. It needs to be nested
109+
within a ``#[AttributeOverrides]`` on the class level.
110+
111+
Required parameters:
112+
113+
- **name**: Name of the association mapping to overwrite.
114+
- **column**: A nested ``#[Column]`` attribute with the overwritten field settings.
115+
116+
Examples:
117+
118+
.. code-block:: php
119+
120+
<?php
121+
use Doctrine\ORM\Mapping\AttributeOverride;
122+
use Doctrine\ORM\Mapping\AttributeOverrides;
123+
use Doctrine\ORM\Mapping\Column;
124+
use Doctrine\ORM\Mapping\Entity;
125+
126+
#[Entity]
127+
#[AttributeOverrides([
128+
new AttributeOverride(
129+
name: "id",
130+
column: new Column(name: "guest_id", type: "integer", length: 140)
131+
),
132+
new AttributeOverride(
133+
name: "name",
134+
column: new Column(name: "guest_name", nullable: false, unique: true, length: 240)
135+
)]
136+
)]
137+
class DDC964Guest extends DDC964User
138+
{
139+
}
140+
52141
.. _attrref_column:
53142

54143
#[Column]
@@ -59,12 +148,12 @@ inside the instance variables PHP DocBlock comment. Any value hold
59148
inside this variable will be saved to and loaded from the database
60149
as part of the lifecycle of the instance variables entity-class.
61150

62-
Required attributes:
151+
Required parameters:
63152

64153
- **type**: Name of the DBAL Type which does the conversion between PHP
65154
and Database representation.
66155

67-
Optional attributes:
156+
Optional parameters:
68157

69158
- **name**: By default the property name is used for the database
70159
column name also, however the ``name`` attribute allows you to
@@ -165,7 +254,7 @@ Examples:
165254
~~~~~~~~
166255
Add caching strategy to a root entity or a collection.
167256

168-
Optional attributes:
257+
Optional parameters:
169258

170259
- **usage**: One of ``READ_ONLY``, ``READ_WRITE`` or ``NONSTRICT_READ_WRITE``, By default this is ``READ_ONLY``.
171260
- **region**: An specific region name
@@ -210,7 +299,7 @@ Example:
210299

211300
This attribute allows you to specify a user-provided class to generate identifiers. This attribute only works when both :ref:`#[Id] <attrref_id>` and :ref:`#[GeneratedValue(strategy: "CUSTOM")] <attrref_generatedvalue>` are specified.
212301

213-
Required attributes:
302+
Required parameters:
214303

215304
- **class**: name of the class which should extend Doctrine\ORM\Id\AbstractIdGenerator
216305

@@ -244,13 +333,13 @@ actually instantiated as.
244333
If this attribute is not specified, the discriminator column defaults
245334
to a string column of length 255 called ``dtype``.
246335

247-
Required attributes:
336+
Required parameters:
248337

249338

250339
- **name**: The column name of the discriminator. This name is also
251340
used during Array hydration as key to specify the class-name.
252341

253-
Optional attributes:
342+
Optional parameters:
254343

255344

256345
- **type**: By default this is string.
@@ -319,7 +408,7 @@ attribute to establish the relationship between the two classes.
319408
The embedded attribute is required on an entity's member variable,
320409
in order to specify that it is an embedded class.
321410

322-
Required attributes:
411+
Required parameters:
323412

324413
- **class**: The embeddable class
325414

@@ -331,7 +420,7 @@ Required attributes:
331420
Required attribute to mark a PHP class as an entity. Doctrine manages
332421
the persistence of all classes marked as entities.
333422

334-
Optional attributes:
423+
Optional parameters:
335424

336425
- **repositoryClass**: Specifies the FQCN of a subclass of the
337426
``EntityRepository``. Use of repositories for entities is encouraged to keep
@@ -368,7 +457,7 @@ conjunction with #[Id].
368457
If this attribute is not specified with ``#[Id]`` the ``NONE`` strategy is
369458
used as default.
370459

371-
Optional attributes:
460+
Optional parameters:
372461

373462
- **strategy**: Set the name of the identifier generation strategy.
374463
Valid values are ``AUTO``, ``SEQUENCE``, ``IDENTITY``, ``UUID``
@@ -424,14 +513,14 @@ Attribute is used on the entity-class level. It provides a hint to the SchemaToo
424513
generate a database index on the specified table columns. It only
425514
has meaning in the ``SchemaTool`` schema generation context.
426515

427-
Required attributes:
516+
Required parameters:
428517

429518
- **name**: Name of the Index
430519
- **fields**: Array of fields. Exactly one of **fields, columns** is required.
431520
- **columns**: Array of columns. Exactly one of **fields, columns** is required.
432521

433522

434-
Optional attributes:
523+
Optional parameters:
435524

436525
- **options**: Array of platform specific options:
437526

@@ -546,9 +635,10 @@ are missing they will be computed considering the field's name and the current
546635

547636
The ``#[InverseJoinColumn]`` is the same as ``#[JoinColumn]`` and is used in the context
548637
of a ``#[ManyToMany]`` attribute declaration to specifiy the details of the join table's
549-
column information used for the join to the inverse entity.
638+
column information used for the join to the inverse entity. This is only required
639+
on PHP 8.0, where nested attributes are not yet supported.
550640

551-
Optional attributes:
641+
Optional parameters:
552642

553643
- **name**: Column name that holds the foreign key identifier for
554644
this relation. In the context of ``#[JoinTable]`` it specifies the column
@@ -596,7 +686,7 @@ details of the database join table. If you do not specify
596686
using the affected table and the column names.
597687

598688
A notable difference to the annotation metadata support, ``#[JoinColumn]``
599-
and ``#[InverseJoinColumn]`` are specified at the property level and are not
689+
and ``#[InverseJoinColumn]`` can be specified at the property level and are not
600690
nested within the ``#[JoinTable]`` attribute.
601691

602692
Required attribute:
@@ -623,14 +713,14 @@ Example:
623713
Defines that the annotated instance variable holds a reference that
624714
describes a many-to-one relationship between two entities.
625715

626-
Required attributes:
716+
Required parameters:
627717

628718

629719
- **targetEntity**: FQCN of the referenced target entity. Can be the
630720
unqualified class name if both classes are in the same namespace.
631721
*IMPORTANT:* No leading backslash!
632722

633-
Optional attributes:
723+
Optional parameters:
634724

635725

636726
- **cascade**: Cascade Option
@@ -659,14 +749,14 @@ additional, optional attribute that has reasonable default
659749
configuration values using the table and names of the two related
660750
entities.
661751

662-
Required attributes:
752+
Required parameters:
663753

664754

665755
- **targetEntity**: FQCN of the referenced target entity. Can be the
666756
unqualified class name if both classes are in the same namespace.
667757
*IMPORTANT:* No leading backslash!
668758

669-
Optional attributes:
759+
Optional parameters:
670760

671761

672762
- **mappedBy**: This option specifies the property name on the
@@ -720,7 +810,7 @@ The ``#[MappedSuperclass]`` attribute cannot be used in conjunction with
720810
``#[Entity]``. See the Inheritance Mapping section for
721811
:doc:`more details on the restrictions of mapped superclasses <inheritance-mapping>`.
722812

723-
Optional attributes:
813+
Optional parameters:
724814

725815
- **repositoryClass**: Specifies the FQCN of a subclass of the EntityRepository.
726816
That will be inherited for all subclasses of that Mapped Superclass.
@@ -756,13 +846,13 @@ be specified. When no
756846
:ref:`#[JoinColumn] <attrref_joincolumn>` is specified it defaults to using the target entity table and
757847
primary key column names and the current naming strategy to determine a name for the join column.
758848

759-
Required attributes:
849+
Required parameters:
760850

761851
- **targetEntity**: FQCN of the referenced target entity. Can be the
762852
unqualified class name if both classes are in the same namespace.
763853
*IMPORTANT:* No leading backslash!
764854

765-
Optional attributes:
855+
Optional parameters:
766856

767857
- **cascade**: Cascade Option
768858
- **fetch**: One of LAZY or EAGER
@@ -786,13 +876,13 @@ Example:
786876
#[OneToMany]
787877
~~~~~~~~~~~~
788878

789-
Required attributes:
879+
Required parameters:
790880

791881
- **targetEntity**: FQCN of the referenced target entity. Can be the
792882
unqualified class name if both classes are in the same namespace.
793883
*IMPORTANT:* No leading backslash!
794884

795-
Optional attributes:
885+
Optional parameters:
796886

797887
- **cascade**: Cascade Option
798888
- **orphanRemoval**: Boolean that specifies if orphans, inverse
@@ -916,11 +1006,11 @@ For use with ``#[GeneratedValue(strategy: "SEQUENCE")]`` this
9161006
attribute allows to specify details about the sequence, such as
9171007
the increment size and initial values of the sequence.
9181008

919-
Required attributes:
1009+
Required parameters:
9201010

9211011
- **sequenceName**: Name of the sequence
9221012

923-
Optional attributes:
1013+
Optional parameters:
9241014

9251015
- **allocationSize**: Increment the sequence by the allocation size
9261016
when its fetched. A value larger than 1 allows optimization for
@@ -954,11 +1044,11 @@ placed on the entity-class level and is optional. If it is
9541044
not specified the table name will default to the entity's
9551045
unqualified classname.
9561046

957-
Required attributes:
1047+
Required parameters:
9581048

9591049
- **name**: Name of the table
9601050

961-
Optional attributes:
1051+
Optional parameters:
9621052

9631053
- **schema**: Name of the schema the table lies in.
9641054

@@ -985,12 +1075,12 @@ generate a database unique constraint on the specified table
9851075
columns. It only has meaning in the SchemaTool schema generation
9861076
context.
9871077

988-
Required attributes:
1078+
Required parameters:
9891079

9901080
- **name**: Name of the Index
9911081
- **columns**: Array of columns.
9921082

993-
Optional attributes:
1083+
Optional parameters:
9941084

9951085
- **options**: Array of platform specific options:
9961086

0 commit comments

Comments
 (0)