我使用一个平台来开发rest API,现在当我在normalize和denormalize中序列化数据时,我发现了一个错误。我对实体使用条令继承,我有类-人抽象类-内容-公共属性,我创建类Partner、Participant和declarant,所有类都为人类扩展,现在当我在Participant类中添加组时,我发现错误,错误是当我在Partner和Declarator属性中添加一个组时,它会显示,但当我还在属性类person中添加一组时,类中的属性参与者未显示
法人实体:
<?php
namespace AppEntity;
use AppRepositoryPersonRepository;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyComponentSerializerAnnotationGroups;
/**
* @ORMEntity(repositoryClass=PersonRepository::class)
* @ORMInheritanceType("JOINED")
* @ORMDiscriminatorColumn(name="discr", type="string")
* @ORMDiscriminatorMap({"partner" = "Partner", "supplier" = "Supplier", "declaring"= "Declaring", "particpant"= "Particpant"})
*/
abstract class Person
{
use ResourceId;
use AbstractEntity;
/**
* @ORMColumn(type="string", length=255)
* @Groups({"readPartner","writePartner","readDeclaring",
* "writeDeclaring","readSupplier","writeSupplier","readParticpant","writeParticpant"})
*/
private $firstName;
/**
* @ORMColumn(type="string", length=255)
* @Groups({"readPartner","writePartner","readDeclaring",
* "writeDeclaring","readSupplier","writeSupplier","readParticpant","writeParticpant"})
*/
private $lastName;
/**
* @ORMColumn(type="string", length=255)
* @Groups({"readPartner","writePartner","readDeclaring",
* "writeDeclaring","readSupplier","writeSupplier"})
*/
private $personalPhone;
/**
* @ORMColumn(type="string", length=255,nullable=true)
* @Groups({"readPartner","writePartner","readDeclaring"
* ,"writeDeclaring","readSupplier","writeSupplier"})
*/
private $professionalPhone;
/**
* @ORMColumn(type="string", length=255, nullable=true)
* @AssertEmail(
* message = "The email '{{ value }}' is not a valid email."
* )
* @Groups({"readPartner","writePartner","readDeclaring",
* "writeDeclaring","readSupplier","writeSupplier"})
*/
private $email;
/**
* @ORMColumn(type="string", length=255)
* @Groups({"readPartner","writePartner","readDeclaring"
* ,"writeDeclaring","readSupplier","writeSupplier"})
*/
private $organization;
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getPersonalPhone(): ?string
{
return $this->personalPhone;
}
public function setPersonalPhone(string $personalPhone): self
{
$this->personalPhone = $personalPhone;
return $this;
}
public function getProfessionalPhone(): ?string
{
return $this->professionalPhone;
}
public function setProfessionalPhone(string $professionalPhone): self
{
$this->professionalPhone = $professionalPhone;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): self
{
$this->email = $email;
return $this;
}
public function getOrganization(): ?string
{
return $this->organization;
}
public function setOrganization(string $organization): self
{
$this->organization = $organization;
return $this;
}
}
参与者实体:
<?php
namespace AppEntity;
use ApiPlatformCoreAnnotationApiResource;
use AppRepositoryParticpantRepository;
use DoctrineORMMapping as ORM;
use SymfonyComponentSerializerAnnotationGroups;
/**
* @ApiResource(
* normalizationContext={"groups"={"readParticpant"}},
* denormalizationContext={"groups"={"writeParticpant"}},
* )
* @ORMEntity(repositoryClass=ParticpantRepository::class)
*/
class Particpant extends Person
{
/**
* @ORMManyToOne(targetEntity=Partner::class)
* @ORMJoinColumn(name="partner_id", referencedColumnName="id", nullable=true)
* @Groups({"readParticpant","writeParticpant"})
*/
protected $partner;
/**
* @ORMManyToOne(targetEntity=Declaring::class)
* @ORMJoinColumn(name="declaring_id", referencedColumnName="id", nullable=true)
* @Groups({"readParticpant","writeParticpant"})
*/
protected $declaring;
/**
* Get the value of partner
*/
public function getPartner(): ?Partner
{
return $this->partner;
}
/**
* Set the value of partner
* @param Partner $partner
* @return self
*/
public function setPartner(Partner $partner): self
{
$this->partner = $partner;
return $this;
}
/**
* Get the value of declaring
*/
public function getDeclaring(): ?Declaring
{
return $this->declaring;
}
/**
* Set the value of declaring
* @param Declaring $declaring
* @return self
*/
public function setDeclaring(Declaring $declaring): self
{
$this->declaring = $declaring;
return $this;
}
}
如何显示个人实体的属性和参与者的属性。。
即使条令很好地支持继承,api平台也不支持。
反序列化忽略父类属性或子类属性。
真正的问题是为什么需要继承
- 添加链接到Person的新实体类
- 使用多态性,如
public function heal(Person $person): void {}
- 收集共同财产
如果是1.,那你就大错特错了。例如,假设您想添加一个Dog类:
/*
* @ApiResource
* @ORMEntity
*/
class Dog
{
/*
* @Column
*/
private $name;
/*
* @var Person
* @ManyToOne(class="Person")
*/
private $owner;
}
即使有一个具体的Person类,序列化也不会像预期的那样工作,IRI生成不会检测到子类。你不会看到几个Dog这样的例子:
{"name": "Bar", "owner": "/participants/1"}
{"name": "Foo", "owner": "/partners/2"}
{"name": "Dummy", "owner": "/declarants/4"}
Api平台将尝试创建包含/persone字符串的IRI。获取/Person仅检索Person属性。
如果是2.,那么您的实体只需要实现一个通用接口,或者您可以使用访问者模式。
如果是3.,常见的方法是使用条令可嵌入对象。它们支持@Groups注释。