I need a little help, I have next scenario:
AbtsractItem File:
<?php
namespace Com;
use JMS\Serializer\Annotation as JMS;
/**
* @JMS\Discriminator(
* field = "objectType",
* map = {
* "part": "Com\Part",
* "complement" : "Com\Complement"
* },
* disabled=true
* )
*/
abstract class AbstractItem
{
protected $objectType;
}
Part File:
<?php
namespace Com;
use JMS\Serializer\Annotation as JMS;
class Part extends AbstractItem
{
/**
* @JMS\Type("string")
* @JMS\XmlElement(cdata=false)
*/
protected $objectType = "Part";
/**
* @JMS\Type("string")
* @JMS\XmlElement(cdata=false)
*/
private $data;
//getters & setters
}
Complement File:
<?php
namespace Com;
use JMS\Serializer\Annotation as JMS;
class Complement extends AbstractItem
{
/**
* @JMS\Type("string")
* @JMS\XmlElement(cdata=false)
*/
protected $objectType = "Complemet";
/**
* @JMS\Type("string")
* @JMS\XmlElement(cdata=false)
*/
private $number;
//getters & setters
}
MyObject File:
<?php
namespace Com;
use JMS\Serializer\Annotation as JMS;
/**
* @JMS\XmlRoot("MyObjects")
*/
class MyObject
{
/**
* @JMS\Type("array<Com\AbstractItem>")
* @JMS\XmlElement(cdata=false)
* @JMS\XmlList(inline=false, entry="item")
*/
private $items;
//getters & setters
}
So I expected something like the next XML:
<MyObjects>
<item>
<objectType>Part</objectType>
<number>1237173</number>
</item>
<item>
<objectType>Complement</objectType>
<data>loremp ipsum...</data>
</item>
</MyObjects>
when I serialized/deserialized in JSON I have not problem, but not like that for XML. I saw the examples for arrays, for polymorphic attributes and the discriminator, but in my case, I need to have a AbstractItem collection, in this way I'm getting a message "Cannot instantiate abstract class Com\AbtsractItem", if my class it wasn't abstract I just get the attribue objectType in the xml cause is serializing a Item but no the children.