現在我有很多物體:
#[ORM\Column(type: 'datetime')]
#[Groups(['article_category:output'])]
/**
* @Timestampable(on="create")
*/
private \DateTime $createdAt;
public function getCreatedAt(): \DateTime
{
return $this->createdAt;
}
我想把它放在 Trait 里面。但是每個物體都有自己的規范化組(如“article_category:output”)。如何使該欄位始終從 API 平臺可用?
uj5u.com熱心網友回復:
我們做了什么:
在你的特質中使用一個組,例如 timestampable
trait TimestampableEntity
{
/**
* ...
* @Groups({"timestampable"})
*/
protected $createdAt;
...
}
然后,每當您想公開時間戳時,將它們添加到您的配置中(您可以將多個組添加到一個操作中)
<?php
/**
* @ApiResource(
* collectionOperations= {
* "get" = {
* "normalization_context"={"groups"={"appointment:read", "timestampable"}},
* },
* },
* )
*/
class Appointment
{
use TimestampableEntity;
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/358349.html
標籤:symfony api-platform.com 化学药品扩展
