API平台模型属性为readOnly [英] API Platform model attributes are readOnly

查看:66
本文介绍了API平台模型属性为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 中,该实体嵌套在 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 设置为readOnly(因此,所有实体的插入都会失败...)

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 相关联.以camelCase格式($ 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平台模型属性为readOnly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆