Reputation: 8046
I have an Doctrine Entity that uses inheritance:
/**
* @ORM\Entity
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\DiscriminatorMap({"video" = "Video", "text" = "Text", "image" = "Image" })
* @ORM\Table(name="item_block_content")
* @ORM\HasLifecycleCallbacks
*
* @Serializer\Discriminator(field = "discr", map = {
* "text": "Namespace\To\Entity\Text",
* "video": "Namespace\To\Entity\Video",
* "image": "Namespace\To\Entity\Image"
* })
*/
class Content implements interfaces\Response {
...
}
I also do have an Item Entity that contains a collection of Content Entities. When I serialize those entities to XML using the JMSSerializer I do see an discr column.
<id />
<metadata />
<metadata_technical />
<created>2013-07-01T11:59:50+02:00</created>
<modified>2013-07-01T12:35:51+02:00</modified>
<title>Some title</title>
<text>Some text</text>
<discr>text</discr>
When I want to deserialize the XML I get the expected result with the exception of the Content classes. They are all \Namespace\To\Entity\Content objects. This way I cannot save the changes to the database.
In the code examples above I already tried the @Discriminator annotation to specify the entity types, but this does not work. Any idea how I can get this to work? I think I'm close, but I'm out of ideas.
EDIT
If I make the following change in the parent class:
/**
* @ORM\OneToMany(targetEntity="Namespace\To\Entity\Content", mappedBy="Block", cascade={"persist"})
*
* @Type("ArrayCollection<Namespace\To\Entity\Content>")
*/
to
/**
* @ORM\OneToMany(targetEntity="Namespace\To\Entity\Content", mappedBy="Block", cascade={"persist"})
*
* @Type("ArrayCollection<Namespace\To\Entity\Text>")
*/
And I only add Text elements its working properly, but adding other elements will (ofcourse) break.
Upvotes: 3
Views: 4167
Reputation: 52493
Looking at BaseSerializationTest and the Fixture Vehicle ... @Discriminator
should do exactly what you're trying to achieve.
There must be something wrong with the way you deserialize your objects. Mapping correctly applied? No Exlusion Strategy or Groups involved?
Upvotes: 0