一个如何在序列化输出中公开鉴别符?

时间:2019-03-14 15:43:49

标签: php symfony jms-serializer

如何在序列化输出中公开鉴别符?

以下内容是根据this example改编的,并由XML reference进行了调整。

我的实体是:

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>

如何在序列化输出中公开鉴别符?

1 个答案:

答案 0 :(得分:0)

如果不使用排除组,则默认情况下将其公开。不幸的是,我希望使用排除组。