简体   繁体   English

Sensio ParamConverter STI 摘要

[英]Sensio ParamConverter STI abstract

Given an Single Table Inheritance for Location -> A and Location -> B给定位置 -> A 和位置 -> B 的单个表 Inheritance

 * @DiscriminatorMap({
 *     "a" = "A",
 *     "b" = "B"
 * })
 * @Discriminator(field = "discr", map = {
 *     "a" = "A",
 *     "b" = "B",
 * })
abstract class Location 

In the Controller, i will send either an A or B type extending Location.在 Controller 中,我将发送 A 或 B 类型的扩展位置。

  /**
   * @Rest\Post("", name="create_l")
   * @ParamConverter("location", converter="fos_rest.request_body")
   */
  public function insert(Location $location): JsonResponse

Doctrine tells me the obvious message it cant instaniiate an abstract class, which is true but it should instead create the subtype. Doctrine 告诉我它无法实例化抽象 class 的明显消息,这是真的,但它应该创建子类型。

If A comes in, it should be converted to A, not instantiiate Location.如果A进来了,应该转换成A,而不是实例化Location。

Any solutions?有什么解决办法吗?

You are not allowed to create an abstract entity class like that.您不能像这样创建抽象实体 class 。 Take a look at the docs on single table inheritance .查看单表 inheritance 上的文档 You can see here that both classes are concrete.您可以在这里看到这两个类都是具体的。 In your situation, it looks like you need some subclasses LocationA LocationB , etc. But to be honest, you should provide more details about your use case, because it seems you might be misusing the inheritance structure.在您的情况下,您似乎需要一些子类LocationA LocationB等。但老实说,您应该提供有关您的用例的更多详细信息,因为您似乎可能误用了 inheritance 结构。

So, after doing some research and testing i fixed it myself.所以,在做了一些研究和测试之后,我自己修复了它。

My core problem was that i mixed two Serializers, JMS and Symfony/Serializer.我的核心问题是我混合了两个序列化器,JMS 和 Symfony/序列化器。 So i decided to go with the Symfony/Serializer as it enabled the creation of the subtypes of abstract classes.所以我决定使用 Symfony/Serializer 来 go,因为它可以创建抽象类的子类型。 This is my setup:这是我的设置:

Imports & Entity:进口和实体:

use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\DiscriminatorColumn;
use Doctrine\ORM\Mapping\InheritanceType;

/**
 * @ORM\Entity(repositoryClass=AbstractParent::class)
 * @InheritanceType("SINGLE_TABLE")
 * @DiscriminatorColumn(name="discr", type="string")
 * @DiscriminatorMap(typeProperty="discr", mapping = {
 *     "a"      = "App\Entity\A",
 *     "b"      = "App\Entity\B",
 * })
 * @ORM\Table(
 *    name="abstract_parent",
 * )
 */
abstract class AbstractParent extends PlaceComputable

When sending a post request to save a subtype:发送 post 请求以保存子类型时:

/**
 * @Rest\Post("", name="create_subtype")
 * @ParamConverter("subtype", converter="fos_rest.request_body")
 */
public function insert(AbstractParent $subtype): JsonResponse
{
 // $subtype is now of Either Type 'A' or 'B'
 // depending on what 'discr' member was passed to the controller
}

Its is now also documented in the Symfony/Serializer documentation as to how convert subtypes of abstract classes.它现在也记录在 Symfony/Serializer 文档中,说明如何转换抽象类的子类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM