[英]symfony doctrine discriminator error serializer
I use a platform to develop rest API, now when I have serialized data in normalize and denormalize I found an error.我使用一个平台开发rest API,现在当我在normalize和denormalize中序列化数据时发现错误。 I use doctrine inheritance for entities, I have class person abstract class content commons attributes and I create class Partner, Participant, and declarant, all classes extend for person class, now when I add groups in Participant class I found error, the error is when I add a group in partner and declarant attributes it displays but when I add also a group in attributes class Person, attributes in class Participant is not display
I use doctrine inheritance for entities, I have class person abstract class content commons attributes and I create class Partner, Participant, and declarant, all classes extend for person class, now when I add groups in Participant class I found error, the error is when我在合作伙伴和声明属性中添加了一个组,它显示,但是当我在属性 class 人员中添加一个组时,class 参与者中的属性不显示
Person entity:个人实体:
<?php
namespace App\Entity;
use App\Repository\PersonRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ORM\Entity(repositoryClass=PersonRepository::class)
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\DiscriminatorMap({"partner" = "Partner", "supplier" = "Supplier", "declaring"= "Declaring", "particpant"= "Particpant"})
*/
abstract class Person
{
use ResourceId;
use AbstractEntity;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"readPartner","writePartner","readDeclaring",
* "writeDeclaring","readSupplier","writeSupplier","readParticpant","writeParticpant"})
*/
private $firstName;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"readPartner","writePartner","readDeclaring",
* "writeDeclaring","readSupplier","writeSupplier","readParticpant","writeParticpant"})
*/
private $lastName;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"readPartner","writePartner","readDeclaring",
* "writeDeclaring","readSupplier","writeSupplier"})
*/
private $personalPhone;
/**
* @ORM\Column(type="string", length=255,nullable=true)
* @Groups({"readPartner","writePartner","readDeclaring"
* ,"writeDeclaring","readSupplier","writeSupplier"})
*/
private $professionalPhone;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\Email(
* message = "The email '{{ value }}' is not a valid email."
* )
* @Groups({"readPartner","writePartner","readDeclaring",
* "writeDeclaring","readSupplier","writeSupplier"})
*/
private $email;
/**
* @ORM\Column(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;
}
}
Particpant entity:参与实体:
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\ParticpantRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ApiResource(
* normalizationContext={"groups"={"readParticpant"}},
* denormalizationContext={"groups"={"writeParticpant"}},
* )
* @ORM\Entity(repositoryClass=ParticpantRepository::class)
*/
class Particpant extends Person
{
/**
* @ORM\ManyToOne(targetEntity=Partner::class)
* @ORM\JoinColumn(name="partner_id", referencedColumnName="id", nullable=true)
* @Groups({"readParticpant","writeParticpant"})
*/
protected $partner;
/**
* @ORM\ManyToOne(targetEntity=Declaring::class)
* @ORM\JoinColumn(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;
}
}
how to display attributs of person entity and attribut of Participant also.. thanks for advanced如何显示人员实体的属性和参与者的属性..感谢您的先进
Even if doctrine supports inheritance very well, api-platform does not.即使 doctrine 很好地支持 inheritance,api-platform 也不支持。
Deserialization ignore either parent class properties or child class properties.反序列化忽略父 class 属性或子 class 属性。
The real question is why do you need inheritance?真正的问题是为什么需要 inheritance?
public function heal(Person $person): void {}
?public function heal(Person $person): void {}
? If it's the 1. , you're quite screwed up.如果是1. ,那你就完蛋了。 For example, suppose you want to add a Dog class:
例如,假设您要添加Dog class:
/*
* @ApiResource
* @ORM\Entity
*/
class Dog
{
/*
* @Column
*/
private $name;
/*
* @var Person
* @ManyToOne(class="Person")
*/
private $owner;
}
Even with a concrete Person class, the serialization won't work as expected, the IRI generation does not detects the child class.即使使用具体的Person class,序列化也不会按预期工作,IRI 生成不会检测到子 class。 You won't see several Dog instances like this:
你不会看到像这样的几个Dog实例:
{"name": "Bar", "owner": "/participants/1"}
{"name": "Foo", "owner": "/partners/2"}
{"name": "Dummy", "owner": "/declarants/4"}
Api-platform will try to create IRIs containing the /persons string. Api-platform 将尝试创建包含/persons字符串的 IRI。 Fetching a /persons retrieve Person properties only.
获取/persons仅检索 Person 属性。
If it's the 2. , your entities just need to implement a common interface, or you can use a visitor pattern.如果是2. ,您的实体只需要实现一个通用接口,或者您可以使用访问者模式。
If it's the 3. , the common way is to use doctrine embeddable objects .如果是3. ,常用的方法是使用doctrine embeddable objects 。 They supports the @Groups annotation.
它们支持@Groups注释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.