Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine Class Inheritance in XML

I'm trying to setup class inheritance using the Doctrine 2 ORM, but am getting errors (even when following their examples). I'm using the console from a Symfony 2 sandbox. The simple example uses the classes Person and Employee; An Employee extends Person.

The error I'm getting when trying to generate the entities is:

[Doctrine\ORM\Mapping\MappingException]

Entity class 'Employee' used in the discriminator map of class 'Application\MyBundle\Entity\Person' does not exist.

The XML being tried is below:

Person

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xsi="http://www.w3.org/2001/XMLSchema-instance"
                  schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

<entity name="Application\MyBundle\Entity\Person" inheritance-type="SINGLE_TABLE">
    <change-tracking-policy>DEFERRED_IMPLICIT</change-tracking-policy>
    <id name="id" type="integer" column="id">
        <generator strategy="AUTO"/>
    </id>

    <discriminator-column name="discr" type="string" />

    <discriminator-map>
        <discriminator-mapping value="employee" class="Employee" />
    </discriminator-map>

    <lifecycle-callbacks/>
</entity>

</doctrine-mapping>

Employee

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xsi="http://www.w3.org/2001/XMLSchema-instance"
                  schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

<entity name="Application\MyBundle\Entity\Employee">
    <change-tracking-policy>DEFERRED_IMPLICIT</change-tracking-policy>
    <id name="id" type="integer" column="id">
        <generator strategy="AUTO"/>
    </id>
    <lifecycle-callbacks/>
</entity>

</doctrine-mapping>

It doesn't seem like many people use XML to configure their Doctrine entities (most examples are using PHP annotations), but it seems the most logical to me so I'd like to know how to do it properly. Has anyone else had this problem, or know of a solution?

A full exception trace is as follows:

Exception trace:
 () at C:\SVN\Symfony\symfony-sandbox\src\vendor\doctrine\lib\Doctrine\ORM\Mappi
ng\MappingException.php:187
 Doctrine\ORM\Mapping\MappingException::invalidClassInDiscriminatorMap() at C:\S
VN\Symfony\symfony-sandbox\src\vendor\doctrine\lib\Doctrine\ORM\Mapping\ClassMet
adataInfo.php:1465
 Doctrine\ORM\Mapping\ClassMetadataInfo->setDiscriminatorMap() at C:\SVN\Symfony
\symfony-sandbox\src\vendor\doctrine\lib\Doctrine\ORM\Mapping\Driver\XmlDriver.p
hp:98
 Doctrine\ORM\Mapping\Driver\XmlDriver->loadMetadataForClass() at C:\SVN\Symfony
\symfony-sandbox\src\vendor\doctrine\lib\Doctrine\ORM\Mapping\Driver\DriverChain
.php:75
 Doctrine\ORM\Mapping\Driver\DriverChain->loadMetadataForClass() at C:\SVN\Symfo
ny\symfony-sandbox\src\vendor\doctrine\lib\Doctrine\ORM\Mapping\ClassMetadataFac
tory.php:280
 Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata() at C:\SVN\Symfony\sym
fony-sandbox\src\vendor\doctrine\lib\Doctrine\ORM\Mapping\ClassMetadataFactory.p
hp:178
 Doctrine\ORM\Mapping\ClassMetadataFactory->getMetadataFor() at C:\SVN\Symfony\s
ymfony-sandbox\src\vendor\doctrine\lib\Doctrine\ORM\Mapping\ClassMetadataFactory
.php:125
 Doctrine\ORM\Mapping\ClassMetadataFactory->getAllMetadata() at C:\SVN\Symfony\s
ymfony-sandbox\src\vendor\symfony\src\Symfony\Bundle\DoctrineBundle\Command\Doct
rineCommand.php:143
 Symfony\Bundle\DoctrineBundle\Command\DoctrineCommand->getBundleMetadatas() at
C:\SVN\Symfony\symfony-sandbox\src\vendor\symfony\src\Symfony\Bundle\DoctrineBun
dle\Command\GenerateEntitiesDoctrineCommand.php:77
 Symfony\Bundle\DoctrineBundle\Command\GenerateEntitiesDoctrineCommand->execute(
) at C:\SVN\Symfony\symfony-sandbox\src\vendor\symfony\src\Symfony\Component\Con
sole\Command\Command.php:150
 Symfony\Component\Console\Command\Command->run() at C:\SVN\Symfony\symfony-sand
box\src\vendor\symfony\src\Symfony\Component\Console\Application.php:184
 Symfony\Component\Console\Application->doRun() at C:\SVN\Symfony\symfony-sandbo
x\src\vendor\symfony\src\Symfony\Bundle\FrameworkBundle\Console\Application.php:
74
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at C:\SVN\Symfony\s
ymfony-sandbox\src\vendor\symfony\src\Symfony\Component\Console\Application.php:
113
 Symfony\Component\Console\Application->run() at C:\SVN\Symfony\symfony-sandbox\
app\console:11
like image 833
blastthisinferno Avatar asked Oct 14 '10 12:10

blastthisinferno


3 Answers

I got the same error.

I figured out what to do:

In your example, you must have the 'Employee' entity already generated.

  1. remove inheritance in Person and add an id in Employee
  2. doctrine:generate:entities
  3. define the xmls as they were at start
  4. doctrine:generate:entities

Worked for me.

like image 59
AdrienBrault Avatar answered Nov 17 '22 02:11

AdrienBrault


You should probably use full class name (with namespaces) when defining the discriminator.

Like:

<discriminator-map>
    <discriminator-mapping value="employee" class="Application\MyBundle\Entity\Employee" />
</discriminator-map>
like image 22
Hakan Deryal Avatar answered Nov 17 '22 03:11

Hakan Deryal


The names of the classes in the discriminator map do not need to be fully qualified if the classes are contained in the same namespace as the entity class on which the discriminator map is applied.

6.2. Single Table Inheritance

like image 1
venimus Avatar answered Nov 17 '22 04:11

venimus