Как можно представить дискриминатор в сериализованном выходе?
Следующее было адаптировано из этого примера и скорректировано с помощью XML-ссылки .
Мои сущности:
namespace Fully\Qualified;
abstract class Vehicle {
public function getId() {return $this->id;}
public function getCommonProperty() {return $this->common_property;}
}
class Car extends Vehicle {
public function getCarProperty() {return $this->car_property;}
}
class Moped extends Vehicle{
public function getMopedProperty() {return $this->moped_property;}
}
Доктрина настроена следующим образом:
<?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="Fully\Qualified\Vehicle" table="vehicle" inheritance-type="JOINED">
<discriminator-column name="dtype" type="string" length="255"/>
<discriminator-map>
<discriminator-mapping value="car" class="Fully\Qualified\Car"/>
<discriminator-mapping value="moped" class="Fully\Qualified\Moped"/>
</discriminator-map>
<id name="id" type="integer" column="id">
<generator strategy="IDENTITY"/>
</id>
<field name="common_property" type="string" column="common_property" length="45"/>
</entity>
</doctrine-mapping>
<?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="Fully\Qualified\Car" table="car">
<field name="car_property" type="string" column="car_property"/>
</entity>
</doctrine-mapping>
<?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="Fully\Qualified\Moped" table="moped">
<field name="moped_property" type="string" column="moped_property"/>
</entity>
</doctrine-mapping>
Мои желаемые результаты выглядят примерно так:
[
{
"id": 1,
"commonProperty": "foo",
"car_property": "foo",
"dtype": "car"
},
{
"id": 2,
"commonProperty": "foo",
"moped_property": "foo",
"dtype": "moped"
},
{
"id": 3,
"commonProperty": "foo",
"moped_property": "foo",
"dtype": "moped"
},
{
"id": 4,
"commonProperty": "foo",
"car_property": "foo",
"dtype": "car"
}
]
Моя попытка настроитьСериализатор работает следующим образом, однако, дискриминатор (то есть "dtype") не включен:
<?xml version="1.0" encoding="UTF-8" ?>
<serializer>
<class name="Fully\Qualified\Vehicle" discriminator-field-name="dtype" discriminator-disabled="false">
<discriminator-class value="car">Fully\Qualified\Car</discriminator-class>
<discriminator-class value="moped">Fully\Qualified\Moped</discriminator-class>
<discriminator-groups>
<group>foo</group>
<group>bar</group>
</discriminator-groups>
<property name="id" groups="foo, bar"/>
<property name="common_property" groups="foo, bar"/>
<property name="dtype" groups="foo, bar"/>
</class>
</serializer>
<?xml version="1.0" encoding="UTF-8" ?>
<serializer>
<class name="Fully\Qualified\Car">
<property name="moped_property" groups="foo, bar"/>
</class>
</serializer>
<?xml version="1.0" encoding="UTF-8" ?>
<serializer>
<class name="Fully\Qualified\Moped">
<property name="moped_property" groups="foo, bar"/>
</class>
</serializer>
Как можно выставить дискриминатор в сериализованном выходе?