我正在用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,而Partenaire嵌套到Ban。因此,通过创建一个新的Professionnel,也应该创建一个新的Partenaire和Ban。
请记住,我的3个实体的所有属性都有get和set函数(当然id函数除外),由于某种原因,第三个实体的属性nom_voie设置为readOnly (因此,所有实体的插入都会失败.)

我不确定这个两级嵌套应该如何使用Groups来表达,我尝试了许多组合,但没有运气.
发布于 2020-12-24 08:32:23
如果您为$nom_voie属性自动生成getter和setter方法,则可能是getNomVoie()和setNomVoie()。
Api平台正在寻找一个setter方法来决定属性是否是只读的,并且不能将$nom_voie与setNomVoie相关联。将属性重命名为camelCase格式($nomVoie)或将setter方法重命名为setNom_voie()
发布于 2019-04-19 07:22:29
如果您想处理只读属性,我认为可以使用规范化/非规范化上下文,而不是"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" } ]发布于 2019-04-19 12:48:59
您必须按如下方式更新您的实体,您不能使用相同的组名来进行规范化和反规范化,现在对于需要提交和读取其值的属性,请将两个组的值添加到它{"ban_read“、"ban_write"}中,例如,对于Id (例如,由于不提交id值而只需要"ban_read”)。
* @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,然后在专业实体中更新denormalizationContext以包含所有其他组。
denormalizationContext={"groups"={"write_professionnel","write_partenaire", "write_ban"}}https://stackoverflow.com/questions/55753672
复制相似问题