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

Allow classname in 'value' attribute of xml discriminator-mapping field #11453

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
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's declare string here, or directly use constructor property promotion

Copy link
Contributor Author

@MatteoFeltrin MatteoFeltrin May 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHPStorm gives these errors:

Typed properties are only allowed since PHP 7.4

Constructor property promotion is only allowed since PHP 8.0

Composer.json

"require": {
        "php": "^7.1 || ^8.0",
        ...,
}


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>