Skip to content

Commit

Permalink
allow classname in 'value' attribute of xml discriminator-mapping field
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoFeltrin committed May 20, 2024
1 parent c5291b4 commit 73e30df
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion doctrine-mapping.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:any minOccurs="0" maxOccurs="unbounded" namespace="##other"/>
</xs:choice>
<xs:attribute name="value" type="xs:NMTOKEN" use="required"/>
<xs:attribute name="value" type="orm:type" use="required"/>
<xs:attribute name="class" type="orm:fqcn" use="required"/>
<xs:anyAttribute namespace="##other"/>
</xs:complexType>
Expand Down
16 changes: 16 additions & 0 deletions tests/Tests/Models/Customer/CustomerType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\Customer;

class CustomerType
{
/** @var string */
public $name;

public function __construct(string $name)
{
$this->name = $name;
}
}
13 changes: 13 additions & 0 deletions tests/Tests/Models/Customer/ExternalCustomer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\Customer;

final class ExternalCustomer extends CustomerType
{
public function __construct(string $name)
{
parent::__construct($name);
}
}
13 changes: 13 additions & 0 deletions tests/Tests/Models/Customer/InternalCustomer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\Customer;

final class InternalCustomer extends CustomerType
{
public function __construct(string $name)
{
parent::__construct($name);
}
}
16 changes: 16 additions & 0 deletions tests/Tests/ORM/Mapping/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\DefaultNamingStrategy;
use Doctrine\ORM\Mapping\DefaultTypedFieldMapper;
use Doctrine\ORM\Mapping\Driver\XmlDriver;
use Doctrine\ORM\Mapping\MappedSuperclass;
use Doctrine\ORM\Mapping\MappingException;
use Doctrine\ORM\Mapping\UnderscoreNamingStrategy;
Expand All @@ -24,6 +25,7 @@
use Doctrine\Tests\Models\CMS;
use Doctrine\Tests\Models\CMS\CmsEmail;
use Doctrine\Tests\Models\Company\CompanyContract;
use Doctrine\Tests\Models\Customer\CustomerType;
use Doctrine\Tests\Models\CustomType\CustomTypeParent;
use Doctrine\Tests\Models\DDC117\DDC117Article;
use Doctrine\Tests\Models\DDC117\DDC117ArticleDetails;
Expand Down Expand Up @@ -1392,6 +1394,20 @@ public function testInvalidCallToGetAssociationMappedByTargetFieldIsDeprecated()

$metadata->getAssociationMappedByTargetField('foo');
}

public function testClassNameMappingDiscriminatorValue(): void
{
$driver = new XmlDriver(
__DIR__ . '/xml',
XmlDriver::DEFAULT_FILE_EXTENSION,
true
);
$xmlElement = $driver->getElement(CustomerType::class);
self::assertEquals(
'Doctrine\Tests\Models\Customer\InternalCustomer',
$xmlElement->children()->{'discriminator-map'}->{'discriminator-mapping'}[0]->attributes()['value']
);
}
}

/** @MappedSuperclass */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?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\Models\Customer\CustomerType" table="customers" inheritance-type="SINGLE_TABLE">
<field name="name" column="name"/>
<discriminator-column name="type" type="string"/>
<discriminator-map>
<discriminator-mapping value="Doctrine\Tests\Models\Customer\InternalCustomer" class="Doctrine\Tests\Models\Customer\InternalCustomer" />
<discriminator-mapping value="Doctrine\Tests\Models\Customer\ExternalCustomer" class="Doctrine\Tests\Models\Customer\ExternalCustomer" />
</discriminator-map>
</entity>
</doctrine-mapping>

0 comments on commit 73e30df

Please sign in to comment.