Metadata export/load not working on subclass (xml/yaml format) #6240

Open
opened 2026-01-22 15:29:25 +01:00 by admin · 4 comments
Owner

Originally created by @mastir on GitHub (May 22, 2019).

Bug Report

Metadata drivers unable to load metadatas exported via Doctrine\ORM\Tools\Export\Driver*Exporter for child classes.

Q A
BC Break no
Version 2.6.3
PHP 7.1

Summary

Exporter tool exports all fields/ids/relations including parent class defined.
Metadata driver loads parent fields/ids/relations first and then throw "Duplicate definition" Exception.

How to reproduce

/**
 * @ORM\Entity
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorMap({"a" = "A", "b" = "B"})
 */
class A{

	/**
 	 * @ORM\Column(name="id", type="integer")
 	 * @ORM\Id
 	 * @ORM\GeneratedValue(strategy="IDENTITY")
 	 */
	public $id;


	/**
	 * @var string
	 * @ORM\Column(name="title", type="string", length=255)
 	 */
	protected $title;

}

/**
 * @ORM\Entity
 */
class B extends A{

	/**
	 * @var string
	 * @ORM\Column(name="text", type="string", length=255)
 	 */
	protected $text;


}

$em = getEntityManagerWithAnnotationDriver();
$path = getMetdataFolderPath();

$cme = new \Doctrine\ORM\Tools\Export\ClassMetadataExporter();
$exporter = $cme->getExporter('xml',$path);
$exporter->setMetadata($em->getMetadataFactory()->getAllMetadata());
$exporter->setOverwriteExistingFiles(true);
$exporter->export();

$config = clone $em->getConfiguration();
$config->setMetadataDriverImpl(new \Doctrine\ORM\Mapping\Driver\XmlDriver($path));
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
$em2 = \Doctrine\ORM\EntityManager::create($em->getConnection(), $config, $em->getConnection()->getEventManager());
$metadata = $em2->getMetadataFactory()->getAllMetadata(); 
Exception

for xml

#Fatal error: 
Uncaught Doctrine\ORM\Mapping\MappingException: Duplicate definition of column 'title' on entity 'Content\B' in a field or discriminator column mapping. in /dir/lib/vendor/Doctrine/ORM/Mapping/MappingException.php:575 
Stack trace: 
#0 /dir/lib/vendor/Doctrine/ORM/Mapping/ClassMetadataInfo.php(1411): Doctrine\ORM\Mapping\MappingException::duplicateColumnName('Content\\B', 'title') #1 /dir/lib/vendor/Doctrine/ORM/Mapping/ClassMetadataInfo.php(2358): Doctrine\ORM\Mapping\ClassMetadataInfo->_validateAndCompleteFieldMapping(Array) 
#2 /dir/lib/vendor/Doctrine/ORM/Mapping/Driver/XmlDriver.php(261): Doctrine\ORM\Mapping\ClassMetadataInfo->mapField(Array) 
#3 /dir/lib/vendor/Doctrine/ORM/Mapping/ClassMetadataFactory.php(151): Doctrine\ORM\Mapping\Driver\XmlDriver->loadMetadataForClass('Content\\B', Object(Doctrine\ORM\Mapping\Cl in /dir/lib/vendor/Doctrine/ORM/Mapping/MappingException.php on line 575

for yaml

Fatal error: Uncaught Doctrine\ORM\Mapping\MappingException: Duplicate definition of column 'id' on entity 'Content\B' in a field or discriminator column mapping. in lib/vendor/Doctrine/ORM/Mapping/MappingException.php:575 
Stack trace: 
#0 lib/vendor/Doctrine/ORM/Mapping/ClassMetadataInfo.php(1411): Doctrine\ORM\Mapping\MappingException::duplicateColumnName('Content\\B', 'id') 
#1 lib/vendor/Doctrine/ORM/Mapping/ClassMetadataInfo.php(2358): Doctrine\ORM\Mapping\ClassMetadataInfo->_validateAndCompleteFieldMapping(Array) 
#2 vendor/Doctrine/ORM/Mapping/Driver/YamlDriver.php(296): Doctrine\ORM\Mapping\ClassMetadataInfo->mapField(Array)
#3 lib/vendor/Doctrine/ORM/Mapping/ClassMetadataFactory.php(151): Doctrine\ORM\Mapping\Driver\YamlDriver->loadMetadataForClass('Content\\B', Object(Doctrine\ORM\Mapping\ClassM in lib/vendor/Doctrine/ORM/Mapping/MappingException.php on line 575

Expected behavior

no exception thrown

Originally created by @mastir on GitHub (May 22, 2019). ### Bug Report Metadata drivers unable to load metadatas exported via Doctrine\ORM\Tools\Export\Driver\*Exporter for child classes. | Q | A |------------ | ------ | BC Break | no | Version | 2.6.3 | PHP | 7.1 #### Summary Exporter tool exports all fields/ids/relations including parent class defined. Metadata driver loads parent fields/ids/relations first and then throw "Duplicate definition" Exception. #### How to reproduce /** * @ORM\Entity * @ORM\InheritanceType("SINGLE_TABLE") * @ORM\DiscriminatorMap({"a" = "A", "b" = "B"}) */ class A{ /** * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ public $id; /** * @var string * @ORM\Column(name="title", type="string", length=255) */ protected $title; } /** * @ORM\Entity */ class B extends A{ /** * @var string * @ORM\Column(name="text", type="string", length=255) */ protected $text; } $em = getEntityManagerWithAnnotationDriver(); $path = getMetdataFolderPath(); $cme = new \Doctrine\ORM\Tools\Export\ClassMetadataExporter(); $exporter = $cme->getExporter('xml',$path); $exporter->setMetadata($em->getMetadataFactory()->getAllMetadata()); $exporter->setOverwriteExistingFiles(true); $exporter->export(); $config = clone $em->getConfiguration(); $config->setMetadataDriverImpl(new \Doctrine\ORM\Mapping\Driver\XmlDriver($path)); $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache); $em2 = \Doctrine\ORM\EntityManager::create($em->getConnection(), $config, $em->getConnection()->getEventManager()); $metadata = $em2->getMetadataFactory()->getAllMetadata(); ##### Exception for xml ``` #Fatal error: Uncaught Doctrine\ORM\Mapping\MappingException: Duplicate definition of column 'title' on entity 'Content\B' in a field or discriminator column mapping. in /dir/lib/vendor/Doctrine/ORM/Mapping/MappingException.php:575 Stack trace: #0 /dir/lib/vendor/Doctrine/ORM/Mapping/ClassMetadataInfo.php(1411): Doctrine\ORM\Mapping\MappingException::duplicateColumnName('Content\\B', 'title') #1 /dir/lib/vendor/Doctrine/ORM/Mapping/ClassMetadataInfo.php(2358): Doctrine\ORM\Mapping\ClassMetadataInfo->_validateAndCompleteFieldMapping(Array) #2 /dir/lib/vendor/Doctrine/ORM/Mapping/Driver/XmlDriver.php(261): Doctrine\ORM\Mapping\ClassMetadataInfo->mapField(Array) #3 /dir/lib/vendor/Doctrine/ORM/Mapping/ClassMetadataFactory.php(151): Doctrine\ORM\Mapping\Driver\XmlDriver->loadMetadataForClass('Content\\B', Object(Doctrine\ORM\Mapping\Cl in /dir/lib/vendor/Doctrine/ORM/Mapping/MappingException.php on line 575 ``` for yaml ``` Fatal error: Uncaught Doctrine\ORM\Mapping\MappingException: Duplicate definition of column 'id' on entity 'Content\B' in a field or discriminator column mapping. in lib/vendor/Doctrine/ORM/Mapping/MappingException.php:575 Stack trace: #0 lib/vendor/Doctrine/ORM/Mapping/ClassMetadataInfo.php(1411): Doctrine\ORM\Mapping\MappingException::duplicateColumnName('Content\\B', 'id') #1 lib/vendor/Doctrine/ORM/Mapping/ClassMetadataInfo.php(2358): Doctrine\ORM\Mapping\ClassMetadataInfo->_validateAndCompleteFieldMapping(Array) #2 vendor/Doctrine/ORM/Mapping/Driver/YamlDriver.php(296): Doctrine\ORM\Mapping\ClassMetadataInfo->mapField(Array) #3 lib/vendor/Doctrine/ORM/Mapping/ClassMetadataFactory.php(151): Doctrine\ORM\Mapping\Driver\YamlDriver->loadMetadataForClass('Content\\B', Object(Doctrine\ORM\Mapping\ClassM in lib/vendor/Doctrine/ORM/Mapping/MappingException.php on line 575 ``` #### Expected behavior no exception thrown
admin added the BugMissing Tests labels 2026-01-22 15:29:25 +01:00
Author
Owner

@mastir commented on GitHub (May 22, 2019):

tested with php driver and it works, problem related to xml/yml drivers only.

@mastir commented on GitHub (May 22, 2019): tested with php driver and it works, problem related to xml/yml drivers only.
Author
Owner

@Ocramius commented on GitHub (May 27, 2019):

What's the dumped XML looking like?

@Ocramius commented on GitHub (May 27, 2019): What's the dumped XML looking like?
Author
Owner

@mastir commented on GitHub (May 28, 2019):

Content.A.xml

<?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="Content\A" table="Content_A" inheritance-type="SINGLE_TABLE">
    <discriminator-column name="dtype" type="string" length="255"/>
    <discriminator-map>
      <discriminator-mapping value="a" class="Content\A"/>
      <discriminator-mapping value="b" class="Content\B"/>
    </discriminator-map>
    <id name="id" type="integer" column="id">
      <generator strategy="IDENTITY"/>
    </id>
    <field name="title" type="string" column="title" length="255" precision="0" scale="0" nullable="false"/>
  </entity>
</doctrine-mapping>

Content.B.xml

<?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="Content\B" table="Content_A" inheritance-type="SINGLE_TABLE">
    <discriminator-column name="dtype" type="string" length="255"/>
    <discriminator-map>
      <discriminator-mapping value="a" class="Content\A"/>
      <discriminator-mapping value="b" class="Content\B"/>
    </discriminator-map>
    <id name="id" type="integer" column="id">
      <generator strategy="IDENTITY"/>
    </id>
    <field name="title" type="string" column="title" length="255" precision="0" scale="0" nullable="false"/>
    <field name="text" type="string" column="text" length="255" precision="0" scale="0" nullable="false"/>
  </entity>
</doctrine-mapping>

Content.A.dcm.yml

Content\A:
    type: entity
    table: Content_A
    inheritanceType: SINGLE_TABLE
    discriminatorColumn:
        name: dtype
        type: string
        length: 255
        fieldName: dtype
    discriminatorMap:
        a: Content\A
        b: Content\B
    id:
        id:
            type: integer
            scale: 0
            length: null
            unique: false
            nullable: false
            precision: 0
            id: true
            generator:
                strategy: IDENTITY
    fields:
        title:
            type: string
            scale: 0
            length: 255
            unique: false
            nullable: false
            precision: 0
    lifecycleCallbacks: {  }

Content.B.dcm.yml

Content\B:
    type: entity
    table: Content_A
    inheritanceType: SINGLE_TABLE
    discriminatorColumn:
        name: dtype
        type: string
        length: 255
        fieldName: dtype
    discriminatorMap:
        a: Content\A
        b: Content\B
    id:
        id:
            type: integer
            scale: 0
            length: null
            unique: false
            nullable: false
            precision: 0
            id: true
            inherited: Content\A
            declared: Content\A
            generator:
                strategy: IDENTITY
    fields:
        title:
            type: string
            scale: 0
            length: 255
            unique: false
            nullable: false
            precision: 0
            inherited: Content\A
            declared: Content\A
        text:
            type: string
            scale: 0
            length: 255
            unique: false
            nullable: false
            precision: 0
    lifecycleCallbacks: {  }
@mastir commented on GitHub (May 28, 2019): Content.A.xml ``` <?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="Content\A" table="Content_A" inheritance-type="SINGLE_TABLE"> <discriminator-column name="dtype" type="string" length="255"/> <discriminator-map> <discriminator-mapping value="a" class="Content\A"/> <discriminator-mapping value="b" class="Content\B"/> </discriminator-map> <id name="id" type="integer" column="id"> <generator strategy="IDENTITY"/> </id> <field name="title" type="string" column="title" length="255" precision="0" scale="0" nullable="false"/> </entity> </doctrine-mapping> ``` Content.B.xml ``` <?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="Content\B" table="Content_A" inheritance-type="SINGLE_TABLE"> <discriminator-column name="dtype" type="string" length="255"/> <discriminator-map> <discriminator-mapping value="a" class="Content\A"/> <discriminator-mapping value="b" class="Content\B"/> </discriminator-map> <id name="id" type="integer" column="id"> <generator strategy="IDENTITY"/> </id> <field name="title" type="string" column="title" length="255" precision="0" scale="0" nullable="false"/> <field name="text" type="string" column="text" length="255" precision="0" scale="0" nullable="false"/> </entity> </doctrine-mapping> ``` Content.A.dcm.yml ``` Content\A: type: entity table: Content_A inheritanceType: SINGLE_TABLE discriminatorColumn: name: dtype type: string length: 255 fieldName: dtype discriminatorMap: a: Content\A b: Content\B id: id: type: integer scale: 0 length: null unique: false nullable: false precision: 0 id: true generator: strategy: IDENTITY fields: title: type: string scale: 0 length: 255 unique: false nullable: false precision: 0 lifecycleCallbacks: { } ``` Content.B.dcm.yml ``` Content\B: type: entity table: Content_A inheritanceType: SINGLE_TABLE discriminatorColumn: name: dtype type: string length: 255 fieldName: dtype discriminatorMap: a: Content\A b: Content\B id: id: type: integer scale: 0 length: null unique: false nullable: false precision: 0 id: true inherited: Content\A declared: Content\A generator: strategy: IDENTITY fields: title: type: string scale: 0 length: 255 unique: false nullable: false precision: 0 inherited: Content\A declared: Content\A text: type: string scale: 0 length: 255 unique: false nullable: false precision: 0 lifecycleCallbacks: { } ```
Author
Owner

@Ocramius commented on GitHub (May 28, 2019):

I'd say the problem is of the exporter, since <field name="title"/> appears twice in an inheritance there. The XML is indeed invalid.

@Ocramius commented on GitHub (May 28, 2019): I'd say the problem is of the exporter, since `<field name="title"/>` appears twice in an inheritance there. The XML is indeed invalid.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6240