API平台-添加新实体与子资源
创始人
2024-09-08 00:30:41
0

要在API平台中添加带有子资源的新实体,您需要以下步骤:

  1. 使用Symfony命令行工具(CLI)创建新实体和子实体:

    php bin/console make:entity Post
    php bin/console make:entity Comment --regenerate
    
  2. 在新实体的注释中添加@ApiResource注释,并使用subresourceOperations参数指定子实体。下面是一个示例:

    /**
     * @ORM\Entity(repositoryClass=PostRepository::class)
     * @ApiResource(
     *  subresourceOperations={
     *      "comments_get_subresource"={
     *          "method"="GET",
     *          "path"="/posts/{id}/comments"
     *      }
     *  }
     * )
     */
    class Post
    {
        // ...
    }
    
  3. 在子实体的注释中添加@ApiResource注释,并使用attributes参数将其与父实体关联。下面是一个示例:

    /**
     * @ORM\Entity(repositoryClass=CommentRepository::class)
     * @ApiResource(
     *  attributes={
     *      "normalization_context"={"groups"={"comment"}}
     *  }
     * )
     */
    class Comment
    {
        /**
         * @ORM\ManyToOne(targetEntity=Post::class, inversedBy="comments")
         * @ORM\JoinColumn(nullable=false)
         */
        private $post;
    
        // ...
    }
    
  4. Post实体中添加一个关联到子实体的属性和一个添加/删除子实体的方法。下面是一个示例:

    /**
     * @ORM\Entity(repositoryClass=PostRepository::class)
     * @ApiResource(
     *  subresourceOperations={
     *      "comments_get_subresource"={
     *          "method"="GET",
     *          "path"="/posts/{id}/comments"
     *      }
     *  }
     * )
     */
    class Post
    {
        /**
         * @ORM\OneToMany(targetEntity

相关内容

热门资讯

热点推荐(轰趴大菠萝)外挂透明... 热点推荐(轰趴大菠萝)外挂透明挂辅助神器(智能ai代打)原来真的有挂(2020已更新)(微博热搜)1...
玩家必看秘籍(wepower)... 玩家必看秘籍(wepower)外挂透明挂辅助脚本(智能ai代打)其实真的有挂(2020已更新)(哔哩...
教程攻略(fishpoker)... 教程攻略(fishpoker)外挂透明挂辅助安装(透视)总是真的有挂(2020已更新)(哔哩哔哩)1...
总算了解(wepower德州)... 总算了解(wepower德州)外挂透明挂辅助工具(智能ai代打)果真真的有挂(2022已更新)(百度...
推荐十款(wpK)外挂透明挂辅... 推荐十款(wpK)外挂透明挂辅助工具(辅助挂)的确是有挂的(2023已更新)(头条)辅助器中分为三种...
一分钟了解(pokerx)外挂... 一分钟了解(pokerx)外挂透明挂辅助黑科技(软件透明挂)的确是有挂的(2021已更新)(抖音)1...
重大消息(德州nzt)外挂透明... 重大消息(德州nzt)外挂透明挂辅助插件(软件透明挂)一般真的有挂(2025已更新)(百度)1、任何...
分享认知(拱趴大菠萝)外挂透明... 分享认知(拱趴大菠萝)外挂透明挂辅助app(智能ai代打)都是真的有挂(2022已更新)(哔哩哔哩)...
教程攻略(Aapoker)外挂... 教程攻略(Aapoker)外挂透明挂辅助软件(软件透明挂)果然真的有挂(2021已更新)(百度)1、...
玩家亲测(红龙软件德州扑克)外... 玩家亲测(红龙软件德州扑克)外挂透明挂辅助挂(智能ai代打)就是真的有挂(2020已更新)(头条)1...