API 平台模型属性是只读的 [英] API Platform model attributes are readOnly
问题描述
我的 API 遇到了这种奇怪的行为:一些属性设置为 readOnly: true
.
I'm struggling with this strange behavior with my API: some of the attributes are set to 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
.
To sum it up, my Professionnel
entity is nested to Partenaire
which is nested to Ban
. So by creating a new Professionnel
a new Partenaire
and Ban
should be created as well.
请记住,我的 3 个实体的所有属性都有 get
和 set
函数(当然 id 除外)...但出于某种原因,属性我的第三个实体的 nom_voie
设置为只读(因此,所有实体的插入都失败了...)
Please keep in mind that all the properties of my 3 entities have get
and set
functions (except id's of course)...but for some reason, the property nom_voie
of my third entity is set to readOnly (and because of that, the insert of all the entities fail...)
我不确定应该如何使用 Groups
来表达这种两级嵌套,我尝试了很多组合,但都没有成功...
I'm not sure how exactly this two-level nesting should be expressed using Groups
I tried many combinations but no luck...
推荐答案
如果您使用php bin/console make:entity"为 $nom_voie 属性自动生成 getter 和 setter 方法;那可能是 getNomVoie() 和 setNomVoie() .
if you automatically generated getter and setter methods for $nom_voie attribute with "php bin/console make:entity" that probably would be getNomVoie() and setNomVoie() .
Api 平台正在寻找一种 setter 方法来确定属性是否为只读且不能将 $nom_voie 与 setNomVoie 关联.以驼峰格式重命名属性 ($nomVoie) 或将 setter 方法重命名为 setNom_voie()
Api platform is looking for a setter method to decide if attribute is readonly and cannot associate $nom_voie with setNomVoie. Either rename attribute in camelCase format ($nomVoie) or rename the setter method to setNom_voie()
这篇关于API 平台模型属性是只读的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!