1

I'm trying to figure out how the superclass mapping works in Doctrine2, and I have some examples to work with, but I've been using the xml mapping to create my Entities and then in turn the database schema. I found one example that showed an 'extends' parameter to the 'entity' tag, but it does not appear to be supported in the current schema.

How do you go about telling the XML driver what classes should extend a mapped superclass?

SW

Scott
  • 7,983
  • 2
  • 26
  • 41

1 Answers1

0

How do you go about telling the XML driver what classes should extend a mapped superclass?

You don't have to :)

You can simply create an abstract class (lets name it My\First\BaseClass) and define a Mapped Superclass in 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
        http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"
>

    <mapped-superclass name="My\First\BaseClass">

        <!-- fields, etc -->

    </mapped-superclass>

</doctrine-mapping>

Next have an Entity class extend this Mapped Superclass. You can even have an Entity class extending a Mapped Superclass which in turn extends another Mapped Superclass.

The point is: Doctrine is smart enough to traverse all XML mapping files in order to determine the complete set of mapping metadata, based on class inheritance. You don't need to specify the graph in XML.

Jasper N. Brouwer
  • 21,517
  • 4
  • 52
  • 76
  • I found a post over on one of the Doctrine sites that basically gave me an answer - it can't! The explanation was that it was a chicken/egg delimma so they left support for defining inheritance out of the xml layer and require you to use the php with annotations to define the relationships, so I have switched to using annotations instead of xml and just creating the entities myself rather than using generate-entities from the command line tool – Scott Jan 08 '15 at 21:08
  • mind you, the problem I was having was specific to getting 'generate-entities' to work properly. It is this which was said to be not supported from the xml configuration. Presumably, you could create the entities yourself (with or without annotation) and use xml config files to describe the schema. – Scott Jan 08 '15 at 21:11
  • Ah! Manually creating an entity was indeed what I was talking about. Generating entities with inheritance is currently not supported, like you found out, but there has been some talk on the ml about implementing it. – Jasper N. Brouwer Jan 08 '15 at 21:51
  • PS: It would have helped if you mentioned you're _generating_ entities in your question ;) – Jasper N. Brouwer Jan 08 '15 at 21:57