我正在用我的API来应对这种奇怪的行为:一些属性设置为readOnly: true
。
编辑:这是我的实体的定义方式
/**
* @ApiResource(
* normalizationContext={"groups"={"read_partenaire"}},
* denormalizationContext={"groups"={"write_partenaire"}}
* )
* @ORM\Entity(repositoryClass="App\Repository\ProfessionnelRepository")
* @ApiFilter(SearchFilter::class, properties={"nom": "partial", "id": "exact"})
*/
class Professionnel
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups({"read_partenaire"})
*/
private $id;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Partenaire", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
* @Groups({"read_partenaire","write_partenaire"})
*
*/
private $partenaire;
/**
* @ORM\Column(type="string", length=4)
* @Groups({"read_partenaire","write_partenaire"})
*
*/
private $civilite;
/**
* @ORM\Column(type="string", length=100)
* @Groups({"read_partenaire","write_partenaire"})
*
*/
private $nom;
/**
* @ORM\Column(type="string", length=100)
* @Groups({"read_partenaire","write_partenaire"})
*
*/
private $prenom;
第二个实体:
/**
* @ApiResource(
* normalizationContext={"groups"={"read_partenaire"}},
* denormalizationContext={"groups"={"write_partenaire"}}
* )
* @ApiFilter(SearchFilter::class, properties={"id": "exact"})
* @ORM\Entity(repositoryClass="App\Repository\PartenaireRepository")
*/
class Partenaire
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups({"read_partenaire"})
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Ban", inversedBy="partenaires", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
* @Groups({"read_partenaire","write_partenaire"})
*/
private $ban;
第三个实体:
/**
* @ApiResource()
* @ORM\Entity(repositoryClass="App\Repository\BanRepository")
*/
class Ban
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups({"read_partenaire"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"read_partenaire","write_partenaire"})
*
*/
private $nom_voie;
总而言之,我的Professionnel
实体嵌套到Partenaire
,后者嵌套到Ban
。因此,还应通过创建新的Professionnel
来创建新的Partenaire
和Ban
。
请记住,我的3个实体的所有属性都具有get
和set
函数(当然,除了id之外)...但是由于某些原因,...的属性nom_voie
我的第三个实体设置为readOnly(因此,所有实体的插入都会失败...)
我不确定应该使用Groups
来表达两层嵌套的确切方式,我尝试了很多组合但没有运气...
答案 0 :(得分:0)
如果您要处理只读属性,我认为您可以使用规范化/非规范化上下文,而不是“ getter / no setter”
Class Address {
//...
/**
* @ORM\Column(type="integer")
* @Groups({"read", "write"})
*/
private $numero;
/**
* @ORM\Column(type="integer")
* @Groups({"read"})
*/
private $code_insee;
public function getNumero(): ?int
{
return $this->numero;
}
public function setNumero(int $numero): self
{
$this->numero = $numero;
return $this;
}
public function getCodeInsee(): ?int
{
return $this->code_insee;
}
public function setCodeInsee(int $code_insee): self
{
$this->code_insee = $code_insee;
return $this;
}
}
和
services:
# ...
resource.Address:
parent: "api.resource"
arguments: [ "AppBundle\Entity\Address" ]
calls:
- method: "initNormalizationContext"
arguments: [ { groups: [ "read" ] } ]
- method: "initDenormalizationContext"
arguments: [ { groups: [ "write" ] } ]
tags: [ { name: "api.resource" } ]
答案 1 :(得分:0)
您必须按以下方式更新您的实体,不能使用相同的组名进行规范化和非规范化,现在对于需要提交并读取其值的属性,稍后将这两个组都添加到其中{“ ban_read”,“ ban_write “},例如,仅需要” ban_read“作为ID,因为您不会提交ID值。
* @ApiResource(
* normalizationContext={"groups"={"ban_read"}, "swagger_definition_name": "read"},
* denormalizationContext={"groups"={"ban_write"}, "swagger_definition_name": "write"}
* )
*/
Class Address {
//...
/**
* @ORM\Column(type="integer")
* @Groups({"ban_read"})
*/
private $numero;
/**
* @ORM\Column(type="integer")
* @Groups({"ban_write","ban_read"})
*/
private $code_insee;
//here you can see they both have identical getter and setter :
public function getNumero(): ?int
{
return $this->numero;
}
public function setNumero(int $numero): self
{
$this->numero = $numero;
return $this;
}
public function getCodeInsee(): ?int
{
return $this->code_insee;
}
public function setCodeInsee(int $code_insee): self
{
$this->code_insee = $code_insee;
return $this;
}
}
更新: 为每个实体创建唯一的组,例如read_ban,write_ban,然后在Professionnel实体中更新denormalizationContext以包括所有其他组。
denormalizationContext={"groups"={"write_professionnel","write_partenaire", "write_ban"}}