Skip to content

Commit 705d88e

Browse files
committedOct 28, 2021
Add XSD "orm:columntoken" type in order to support reserved words in column names
1 parent 3271d8f commit 705d88e

5 files changed

+101
-3
lines changed
 

‎doctrine-mapping.xsd

+9-3
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@
295295
</xs:choice>
296296
<xs:attribute name="name" type="xs:NMTOKEN" use="required" />
297297
<xs:attribute name="type" type="xs:NMTOKEN" default="string" />
298-
<xs:attribute name="column" type="xs:NMTOKEN" />
298+
<xs:attribute name="column" type="orm:columntoken" />
299299
<xs:attribute name="length" type="xs:NMTOKEN" />
300300
<xs:attribute name="unique" type="xs:boolean" default="false" />
301301
<xs:attribute name="nullable" type="xs:boolean" default="false" />
@@ -402,7 +402,7 @@
402402
</xs:choice>
403403
<xs:attribute name="name" type="xs:NMTOKEN" use="required" />
404404
<xs:attribute name="type" type="xs:NMTOKEN" />
405-
<xs:attribute name="column" type="xs:NMTOKEN" />
405+
<xs:attribute name="column" type="orm:columntoken" />
406406
<xs:attribute name="length" type="xs:NMTOKEN" />
407407
<xs:attribute name="association-key" type="xs:boolean" default="false" />
408408
<xs:attribute name="column-definition" type="xs:string" />
@@ -497,6 +497,12 @@
497497
</xs:restriction>
498498
</xs:simpleType>
499499

500+
<xs:simpleType name="columntoken" id="columntoken">
501+
<xs:restriction base="xs:token">
502+
<xs:pattern value="[-._:A-Za-z0-9`]+" id="columntoken.pattern"/>
503+
</xs:restriction>
504+
</xs:simpleType>
505+
500506
<xs:complexType name="many-to-many">
501507
<xs:choice minOccurs="0" maxOccurs="unbounded">
502508
<xs:element name="cache" type="orm:cache" minOccurs="0" maxOccurs="1"/>
@@ -612,7 +618,7 @@
612618
<xs:any minOccurs="0" maxOccurs="unbounded" namespace="##other"/>
613619
</xs:choice>
614620
<xs:attribute name="type" type="xs:NMTOKEN" default="string" />
615-
<xs:attribute name="column" type="xs:NMTOKEN" />
621+
<xs:attribute name="column" type="orm:columntoken" />
616622
<xs:attribute name="length" type="xs:NMTOKEN" />
617623
<xs:attribute name="unique" type="xs:boolean" default="false" />
618624
<xs:attribute name="nullable" type="xs:boolean" default="false" />

‎tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php

+46
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,13 @@ public function testDiscriminatorColumnDefaultName(): void
11191119
$class = $this->createClassMetadata(SingleTableEntityIncompleteDiscriminatorColumnMapping::class);
11201120
self::assertEquals('dtype', $class->discriminatorColumn['name']);
11211121
}
1122+
1123+
public function testReservedWordInTableColumn(): void
1124+
{
1125+
$metadata = $this->createClassMetadata(ReservedWordInTableColumn::class);
1126+
1127+
self::assertSame('count', $metadata->getFieldMapping('count')['columnName']);
1128+
}
11221129
}
11231130

11241131
/**
@@ -1774,3 +1781,42 @@ class SingleTableEntityIncompleteDiscriminatorColumnMappingSub1 extends SingleTa
17741781
class SingleTableEntityIncompleteDiscriminatorColumnMappingSub2 extends SingleTableEntityIncompleteDiscriminatorColumnMapping
17751782
{
17761783
}
1784+
1785+
/** @Entity */
1786+
#[ORM\Entity]
1787+
class ReservedWordInTableColumn
1788+
{
1789+
/**
1790+
* @var int
1791+
* @Id
1792+
* @Column(type="integer")
1793+
* @GeneratedValue(strategy="NONE")
1794+
*/
1795+
#[ORM\Id, ORM\Column(type: 'integer'), ORM\GeneratedValue(strategy: 'NONE')]
1796+
public $id;
1797+
1798+
/**
1799+
* @var string|null
1800+
* @Column(name="`count`", type="integer")
1801+
*/
1802+
#[ORM\Column(name: '`count`', type: 'integer')]
1803+
public $count;
1804+
1805+
public static function loadMetadata(ClassMetadataInfo $metadata): void
1806+
{
1807+
$metadata->mapField(
1808+
[
1809+
'id' => true,
1810+
'fieldName' => 'id',
1811+
'type' => 'integer',
1812+
]
1813+
);
1814+
$metadata->mapField(
1815+
[
1816+
'fieldName' => 'count',
1817+
'type' => 'integer',
1818+
'columnName' => '`count`',
1819+
]
1820+
);
1821+
}
1822+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
$metadata->mapField(
6+
[
7+
'id' => true,
8+
'fieldName' => 'id',
9+
'type' => 'integer',
10+
]
11+
);
12+
$metadata->mapField(
13+
[
14+
'fieldName' => 'count',
15+
'type' => 'integer',
16+
'columnName' => '`count`',
17+
]
18+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
6+
https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
7+
8+
<entity name="Doctrine\Tests\ORM\Mapping\ReservedWordInTableColumn">
9+
10+
<id name="id" type="integer" column="id">
11+
<generator strategy="NONE"/>
12+
</id>
13+
14+
<field name="count" column="`count`" type="integer"/>
15+
16+
</entity>
17+
18+
</doctrine-mapping>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Doctrine\Tests\ORM\Mapping\ReservedWordInTableColumn:
2+
type: entity
3+
id:
4+
id:
5+
generator:
6+
strategy: NONE
7+
fields:
8+
count:
9+
type: integer
10+
column: '`count`'

0 commit comments

Comments
 (0)
Please sign in to comment.