Skip to content

Commit e94e1ab

Browse files
committed
Add index mapping to Column
Adds a new option to Column mapping to add indexes to class fields directly instead of having to use the Index() class attribute. This allows users to define indexes in traits where access to the class isn't available. Fixes #11982
1 parent 4664373 commit e94e1ab

File tree

15 files changed

+128
-4
lines changed

15 files changed

+128
-4
lines changed

docs/en/reference/attributes-reference.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ Optional parameters:
175175
- **unique**: Boolean value to determine if the value of the column
176176
should be unique across all rows of the underlying entities table.
177177

178+
- **index**: Boolean value to generate an index for this column.
179+
For more advanced usages, take a look at :ref:`#[Index] <attrref_index>`.
180+
If not specified, default value is ``false``.
181+
178182
- **nullable**: Determines if NULL values allowed for this column.
179183
If not specified, default value is ``false``.
180184

@@ -245,6 +249,9 @@ Examples:
245249
#[Column(type: "string", length: 32, unique: true, nullable: false)]
246250
protected $username;
247251
252+
#[Column(type: "string", index: true)]
253+
protected $firstName;
254+
248255
#[Column(type: "string", columnDefinition: "CHAR(2) NOT NULL")]
249256
protected $country;
250257

docs/en/reference/php-mapping.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ Field & Association Getters
209209

210210
- ``isUniqueField($fieldName)``
211211
- ``isNullable($fieldName)``
212+
- ``isIndexed($fieldName)``
212213
- ``getColumnName($fieldName)``
213214
- ``getFieldMapping($fieldName)``
214215
- ``getAssociationMapping($fieldName)``

docs/en/reference/xml-mapping.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ of several common elements:
112112
113113
<indexes>
114114
<index name="name_idx" columns="name"/>
115-
<index columns="user_email"/>
116115
</indexes>
117116
118117
<unique-constraints>
@@ -131,7 +130,7 @@ of several common elements:
131130
</id>
132131
133132
<field name="name" column="name" type="string" length="50" nullable="true" unique="true" />
134-
<field name="email" column="user_email" type="string" column-definition="CHAR(32) NOT NULL" />
133+
<field name="email" column="user_email" type="string" index="true" column-definition="CHAR(32) NOT NULL" />
135134
136135
<one-to-one field="address" target-entity="Address" inversed-by="user">
137136
<cascade><cascade-remove /></cascade>
@@ -255,6 +254,8 @@ Optional attributes:
255254
only.
256255
- unique - Should this field contain a unique value across the
257256
table? Defaults to false.
257+
- index - Should an index be created for this column? Defaults to
258+
false.
258259
- nullable - Should this field allow NULL as a value? Defaults to
259260
false.
260261
- insertable - Should this field be inserted? Defaults to true.

doctrine-mapping.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@
243243
<xs:attribute name="length" type="xs:NMTOKEN" />
244244
<xs:attribute name="unique" type="xs:boolean" default="false" />
245245
<xs:attribute name="nullable" type="xs:boolean" default="false" />
246+
<xs:attribute name="index" type="xs:boolean" default="false" />
246247
<xs:attribute name="insertable" type="xs:boolean" default="true" />
247248
<xs:attribute name="updatable" type="xs:boolean" default="true" />
248249
<xs:attribute name="generated" type="orm:generated-type" default="NEVER" />

src/Mapping/Builder/FieldBuilder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ public function unique(bool $flag = true): static
6464
return $this;
6565
}
6666

67+
/**
68+
* Sets indexed.
69+
*
70+
* @return $this
71+
*/
72+
public function index(bool $flag = true): static
73+
{
74+
$this->mapping['index'] = $flag;
75+
76+
return $this;
77+
}
78+
6779
/**
6880
* Sets column name.
6981
*

src/Mapping/ClassMetadata.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,13 @@ public function isNullable(string $fieldName): bool
10561056
return $mapping !== false && isset($mapping->nullable) && $mapping->nullable;
10571057
}
10581058

1059+
public function isIndexed(string $fieldName): bool
1060+
{
1061+
$mapping = $this->getFieldMapping($fieldName);
1062+
1063+
return isset($mapping->index) && $mapping->index;
1064+
}
1065+
10591066
/**
10601067
* Gets a column name for a field name.
10611068
* If the column name for the field cannot be found, the given field name

src/Mapping/Column.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function __construct(
3131
public readonly array $options = [],
3232
public readonly string|null $columnDefinition = null,
3333
public readonly string|null $generated = null,
34+
public readonly bool $index = false,
3435
) {
3536
}
3637
}

src/Mapping/Driver/AttributeDriver.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@ private function joinColumnToArray(Mapping\JoinColumn|Mapping\InverseJoinColumn
710710
* length: int,
711711
* unique: bool,
712712
* nullable: bool,
713+
* index: bool,
713714
* precision: int,
714715
* enumType?: class-string,
715716
* options?: mixed[],
@@ -726,6 +727,7 @@ private function columnToArray(string $fieldName, Mapping\Column $column): array
726727
'length' => $column->length,
727728
'unique' => $column->unique,
728729
'nullable' => $column->nullable,
730+
'index' => $column->index,
729731
'precision' => $column->precision,
730732
];
731733

src/Mapping/Driver/XmlDriver.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ private function joinColumnToArray(SimpleXMLElement $joinColumnElement): array
754754
* scale?: int,
755755
* unique?: bool,
756756
* nullable?: bool,
757+
* index?: bool,
757758
* notInsertable?: bool,
758759
* notUpdatable?: bool,
759760
* enumType?: string,
@@ -792,6 +793,10 @@ private function columnToArray(SimpleXMLElement $fieldMapping): array
792793
$mapping['unique'] = $this->evaluateBoolean($fieldMapping['unique']);
793794
}
794795

796+
if (isset($fieldMapping['index'])) {
797+
$mapping['index'] = $this->evaluateBoolean($fieldMapping['index']);
798+
}
799+
795800
if (isset($fieldMapping['nullable'])) {
796801
$mapping['nullable'] = $this->evaluateBoolean($fieldMapping['nullable']);
797802
}

src/Mapping/FieldMapping.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ final class FieldMapping implements ArrayAccess
4242
public int|null $scale = null;
4343
/** Whether a unique constraint should be generated for the column. */
4444
public bool|null $unique = null;
45+
/** Whether an index should be generated for the column. */
46+
public bool|null $index = null;
4547
/**
4648
* @var class-string|null This is set when the field is inherited by this
4749
* class from another (inheritance) parent <em>entity</em> class. The value
@@ -93,6 +95,7 @@ public function __construct(
9395
* length?: int|null,
9496
* id?: bool|null,
9597
* nullable?: bool|null,
98+
* index?: bool|null,
9699
* notInsertable?: bool|null,
97100
* notUpdatable?: bool|null,
98101
* columnDefinition?: string|null,

0 commit comments

Comments
 (0)