【发布时间】:2020-02-08 21:53:13
【问题描述】:
我有一个在 Symfony 5 和 API 平台下开发的项目。但是,我遇到了一个非常奇怪的问题。当我将序列化或反序列化组添加到我的资源并尝试恢复集合甚至项目时,所有字段都不会出现在响应中。
这是我的资源定义
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ApiResource(
* attributes={"security"="is_granted('ROLE_USER')"},
* normalizationContext={
* "groups"={"pelerins_read"}
* },
* collectionOperations={
* "get"={"security"="is_granted('ROLE_INSCRIPTION') or is_granted('ROLE_ENCADREUR')", "security_message"="Vous n'êtes autorisés à consulter cette ressource"},
* "post"={"security"="is_granted('ROLE_INSCRIPTION')", "security_message"="Vous n'êtes autorisés à consulter cette ressource"}
* },
* itemOperations={
* "get"={"security"="is_granted('ROLE_INSCRIPTION') or is_granted('ROLE_ENCADREUR')"},
* "put"={"security"="is_granted('ROLE_INSCRIPTION')"}
* }
* )
* @ORM\Entity(repositoryClass="App\Repository\PelerinRepository")
*/
class Pelerin
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups({"pelerins_read", "pelerins_read", "etat_sante_read"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank(message="Le nom de famille du pèlerin est recquis.")
* @Groups({"pelerins_read", "pelerins_read", "etat_sante_read"})
*/
private $nom;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank(message="Le prénom du pèlerin est recquis.")
* @Groups({"pelerins_read", "pelerins_read", "etat_sante_read"})
*/
private $prenom;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank(message="Le sexe du pèlerin est recquis.")
* @Assert\Choice(choices={"M", "F"}, message="Le sexe doit un caractère entre M et F")
* @Groups({"pelerins_read"})
*/
private $sexe;
/**
* @ORM\Column(type="datetime")
* @Assert\NotBlank(message="La date de naissance du pèlerin est recquise.")
* @Groups({"pelerins_read"})
*/
private $date_naissance;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank(message="Le lieu de naissance du pèlerin est recquis.")
* @Groups({"pelerins_read"})
*/
private $lieu_naissance;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank(message="La résidence du pèlerin est recquise.")
* @Groups({"pelerins_read"})
*/
private $residence;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"pelerins_read"})
*/
private $numero_telephone;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\Email(message="Le format de l'adresse email saisie n'est pas correct")
* @Groups({"pelerins_read"})
*/
private $email;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"pelerins_read"})
*/
private $numero_whatsapp;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\NotBlank(message="Le numéro du passeport du pèlerin est recquis")
* @Groups({"pelerins_read"})
*/
private $numero_passeport;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\NotBlank(message="La nature du passeport du pèlerin est recquis")
* @Groups({"pelerins_read"})
*/
private $nature_passeport;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Formule", inversedBy="pelerins")
* @ORM\JoinColumn(nullable=false)
* @Groups({"pelerins_read"})
* @Assert\NotBlank(message="Formule de voyage recquise pour continuer l'opération")
*/
private $formule;
/**
* @ORM\Column(type="datetime")
* @Groups({"pelerins_read"})
* @Assert\NotBlank(message="La date d'inscription est requise")
*/
private $date_inscription;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"pelerins_read"})
*/
private $inscrit_par;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"pelerins_read"})
*/
private $numero_inscrit_par;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"pelerins_read"})
*/
private $en_cas_urgence;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"pelerins_read"})
*/
private $numero_en_cas_urgence;
/**
* @ORM\Column(type="string", length=500, nullable=true)
* @Groups({"pelerins_read"})
*/
private $commentaire;
/**
* @ORM\Column(type="datetime")
* @Groups({"pelerins_read"})
*/
private $created_at;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
* @ORM\JoinColumn(nullable=false)
* @Groups({"pelerins_read"})
*/
private $created_by;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Groups({"pelerins_read"})
*/
private $updated_at;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
* @Groups({"pelerins_read"})
*/
private $updated_by;
/**
* @ORM\Column(type="boolean")
* @Groups({"pelerins_read"})
*/
private $is_deleted;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Groups({"pelerins_read"})
*/
private $deleted_at;
你可以看到这张图片,只有
{
"nom": "string",
"prenom": "string",
"sexe": "string",
"residence": "string",
"email": "string",
"formule": "string",
"commentaire": "string"
}
正在出现。
Result after adding deserialisation groups
序列化组也是如此。看这张照片 Result after serialization groups 我们有
{
"@id": "/api/pelerins/7",
"@type": "Pelerin",
"id": 7,
"nom": "Knox",
"prenom": "Tamara",
"sexe": "M",
"residence": "Chagai",
"email": "nunc.id@esttempor.ca",
"formule": "/api/formules/1",
"commentaire": null
}
所以我想知道我能做些什么来解决这个错误? 谢谢
【问题讨论】:
标签: php api symfony serialization