Symfony API 中缺少日期 属性

Missing date property in Symfony API

我正在使用 ApiPlatform 在 Symfony 5 中构建一个 api,但我无法通过 api 获取日期 属性 评论实体子资源。我得到 publishedAt 当我得到 all 评论时,但如果我得到一个特定 post 的评论,它就不起作用了。 我将 publishedAt 包含在同一个 normalization_context 组中,用于 subresourceOperations,然后是其他组。我做错了什么?

评论实体:

/**
 * @ApiResource(
 *      attributes={
 *          "order"={"published_at": "DESC"}, 
 *          "pagination_client_enabled"=true, 
 *          "pagination_client_items_per_page"=true
 *      },
 *      itemOperations={
 *          "get",
 *          "put"={
 *              "access_control"="is_granted('ROLE_EDITOR') or is_granted('ROLE_SUBSCRIBER') and object.getUser() == user"
 *          }
 *      },
 *      collectionOperations={
 *          "get",
 *          "post"={
 *              "access_control"="is_granted('ROLE_SUBSCRIBER')",
 *              "normalization_context"={
 *                 "groups"={"get-recipes-comments"}
 *              }
 *          },
 *      },
 *      denormalizationContext={
 *          "groups"={"post"}
 *      },
 *     subresourceOperations={
 *         "api_recipes_comments_get_subresource"={
 *              "method"="GET",
 *              "normalization_context"={
 *                  "groups"={"get-recipes-comments"}
 *              }
 *         }
 *     }
 * )
 * @ORM\Entity(repositoryClass=CommentsRepository::class)
 */
class Comments implements AuthoredEntityInterface
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"get-recipes-comments"})
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity=Users::class, inversedBy="comments")
     * @ORM\JoinColumn(nullable=false)
     * @Groups({"get-recipes-comments"})
     */
    private $user;

    /**
     * @ORM\ManyToOne(targetEntity=Recipes::class, inversedBy="comments")
     * @Groups({"post"})
     */
    private $recipe;

    /**
     * @ORM\Column(type="smallint", nullable=true)
     * @Groups({"post", "get-recipes-comments"})
     */
    private $rating;

    /**
     * @ORM\Column(type="text")
     * @Groups({"post", "get-recipes-comments"})
     */
    private $content;

    /**
     * @var \DateTime
     * @ORM\Column(type="datetime")
     * @Groups({"get-recipes-comments"})
     */
    private $published_at;

    public function __construct()
    {
        $this->published_at = new \DateTime();
    } ...........

api结果:

{
    "@context": "/api/contexts/Comments",
    "@id": "/api/recipes/520/comments",
    "@type": "hydra:Collection",
    "hydra:member": [
        {
            "@id": "/api/comments/1341",
            "@type": "Comments",
            "id": 1341,
            "user": {
                "@id": "/api/users/61",
                "@type": "Users",
                "usergroup": "Super Administrator",
                "username": "superadmin"
            },
            "rating": null,
            "content": "new comment"
        }
    ],
    "hydra:totalItems": 1
}

请使用私人$publishedAt;而不是私人 $published_at;