Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add XSD "orm:columntoken" type in order to support reserved words in column names #9123

Merged
merged 1 commit into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions doctrine-mapping.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@
</xs:choice>
<xs:attribute name="name" type="xs:NMTOKEN" use="required" />
<xs:attribute name="type" type="xs:NMTOKEN" default="string" />
<xs:attribute name="column" type="xs:NMTOKEN" />
<xs:attribute name="column" type="orm:columntoken" />
<xs:attribute name="length" type="xs:NMTOKEN" />
<xs:attribute name="unique" type="xs:boolean" default="false" />
<xs:attribute name="nullable" type="xs:boolean" default="false" />
Expand Down Expand Up @@ -402,7 +402,7 @@
</xs:choice>
<xs:attribute name="name" type="xs:NMTOKEN" use="required" />
<xs:attribute name="type" type="xs:NMTOKEN" />
<xs:attribute name="column" type="xs:NMTOKEN" />
<xs:attribute name="column" type="orm:columntoken" />
<xs:attribute name="length" type="xs:NMTOKEN" />
<xs:attribute name="association-key" type="xs:boolean" default="false" />
<xs:attribute name="column-definition" type="xs:string" />
Expand Down Expand Up @@ -497,6 +497,12 @@
</xs:restriction>
</xs:simpleType>

<xs:simpleType name="columntoken" id="columntoken">
<xs:restriction base="xs:token">
<xs:pattern value="[-._:A-Za-z0-9`]+" id="columntoken.pattern"/>
</xs:restriction>
</xs:simpleType>

<xs:complexType name="many-to-many">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="cache" type="orm:cache" minOccurs="0" maxOccurs="1"/>
Expand Down Expand Up @@ -612,7 +618,7 @@
<xs:any minOccurs="0" maxOccurs="unbounded" namespace="##other"/>
</xs:choice>
<xs:attribute name="type" type="xs:NMTOKEN" default="string" />
<xs:attribute name="column" type="xs:NMTOKEN" />
<xs:attribute name="column" type="orm:columntoken" />
<xs:attribute name="length" type="xs:NMTOKEN" />
<xs:attribute name="unique" type="xs:boolean" default="false" />
<xs:attribute name="nullable" type="xs:boolean" default="false" />
Expand Down
46 changes: 46 additions & 0 deletions tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,13 @@ public function testDiscriminatorColumnDefaultName(): void
$class = $this->createClassMetadata(SingleTableEntityIncompleteDiscriminatorColumnMapping::class);
self::assertEquals('dtype', $class->discriminatorColumn['name']);
}

public function testReservedWordInTableColumn(): void
{
$metadata = $this->createClassMetadata(ReservedWordInTableColumn::class);

self::assertSame('count', $metadata->getFieldMapping('count')['columnName']);
}
}

/**
Expand Down Expand Up @@ -1774,3 +1781,42 @@ class SingleTableEntityIncompleteDiscriminatorColumnMappingSub1 extends SingleTa
class SingleTableEntityIncompleteDiscriminatorColumnMappingSub2 extends SingleTableEntityIncompleteDiscriminatorColumnMapping
{
}

/** @Entity */
#[ORM\Entity]
class ReservedWordInTableColumn
{
/**
* @var int
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="NONE")
*/
#[ORM\Id, ORM\Column(type: 'integer'), ORM\GeneratedValue(strategy: 'NONE')]
public $id;

/**
* @var string|null
* @Column(name="`count`", type="integer")
*/
#[ORM\Column(name: '`count`', type: 'integer')]
public $count;

public static function loadMetadata(ClassMetadataInfo $metadata): void
{
$metadata->mapField(
[
'id' => true,
'fieldName' => 'id',
'type' => 'integer',
]
);
$metadata->mapField(
[
'fieldName' => 'count',
'type' => 'integer',
'columnName' => '`count`',
]
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

$metadata->mapField(
[
'id' => true,
'fieldName' => 'id',
'type' => 'integer',
]
);
$metadata->mapField(
[
'fieldName' => 'count',
'type' => 'integer',
'columnName' => '`count`',
]
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

<entity name="Doctrine\Tests\ORM\Mapping\ReservedWordInTableColumn">

<id name="id" type="integer" column="id">
<generator strategy="NONE"/>
</id>

<field name="count" column="`count`" type="integer"/>

</entity>

</doctrine-mapping>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Doctrine\Tests\ORM\Mapping\ReservedWordInTableColumn:
type: entity
id:
id:
generator:
strategy: NONE
fields:
count:
type: integer
column: '`count`'