API平台模型属性是readOnly

问题描述 投票:1回答:2

我正在使用我的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,也应该创建一个新的PartenaireBan

请记住,我的3个实体的所有属性都有getset函数(当然除了id)...但由于某种原因,我的第三个实体的属性nom_voie被设置为readOnly(因此,插入所有实体都失败了......)Here is the entity documentation I get

我不确定这个两级嵌套究竟应该用Groups来表达我试过很多组合但没有运气......

symfony api-platform.com
2个回答
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" } ]

0
投票

您必须按照以下方式更新您的实体,您无法使用相同的组名称进行规范化和非规范化,现在对于您需要提交的属性并稍后读取其值,将两个组添加到其中{“ban_read”,“ban_write”},例如,对于Id,只需要“ban_read”,因为您不会提交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"}}
© www.soinside.com 2019 - 2024. All rights reserved.