簡體   English   中英

如何在Symfony4 API平台中隱藏實體屬性

[英]How to hide an entity property in Symfony4 api-platform

我有這個實體

/**
* @ApiResource(
*       collectionOperations={
*           "get"={
*               "access_control"="is_granted('IS_AUTHENTICATED_FULLY')"
*           },
*           "post"={
*               "access_control"="is_granted('IS_AUTHENTICATED_FULLY')"
*           }
*       },
*       itemOperations={
*       "get"={
*               "access_control"="is_granted('ROLE_ADMIN') or object.getUser() == user"
*           },
*           "put"={
*               "access_control"="is_granted('ROLE_ADMIN') or object.getUser() == user"
*          },
*           "delete"={
*               "access_control"="is_granted('ROLE_ADMIN') or object.getUser() == user"
*          }
*       }
* )
* @ORM\Entity(repositoryClass="App\Repository\FeedRepository")
*/
class Feed implements AuthoredEntityInterface
{
/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="feeds")
 * @ORM\JoinColumn(nullable=false)
 */
private $user;

/**
 * @ORM\Column(type="string", length=255)
 */
private $name;

/**
 * @ORM\Column(type="string", length=2083, unique=true)
 */
private $url;

//various getters and setters

}

相關的用戶實體實現UserInterface。 該實體實現了一個接口,該接口用於使用記錄的用戶自動填充用戶字段。

自動生成的/ api / feeds POST端點需要3個參數:用戶,名稱和url。

我想從端點中排除參數用戶(因為在內部自動生成)。 我知道我不能使用它,但是這在測試我收到此消息的地方引起了問題:

提供的值無效(無效的IRI?)

謝謝

Symfony序列化程序支持@Groups批注,該批注支持您根據給定的組隱藏或顯示字段。

API平台文檔中有一個示例https://api-platform.com/docs/core/serialization/#using-serialization-groups

/**
 * @ApiResource(
 *     normalizationContext={"groups"={"read"}},
 *     denormalizationContext={"groups"={"write"}}
 * )
 */
class Book
{
    /**
     * @Groups({"read", "write"})
     */
    public $name;

    /**
     * @Groups("write")
     */
    public $author;

    // ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM