【问题标题】:Symfony same @groups between two table with ManyToMany relationsSymfony 在具有 ManyToMany 关系的两个表之间具有相同的 @groups
【发布时间】:2021-06-04 07:16:49
【问题描述】:

我希望在具有多对多关系的两个表之间拥有相同的@groups:当我进入 API 平台 .../api/tags/1 时,我只收到没有“标签”的那个。

{
  "id": 1,
  "title": "A ce monde que tu fais"
}

应用\实体\歌曲

    /**
     * @Groups({"song:read", "song:write"})
     * @ORM\ManyToMany(targetEntity=Tag::class, inversedBy="songs", cascade={"persist"})
     * @ORM\JoinTable(
     *  name="song_tag",
     *  joinColumns={
     *      @ORM\JoinColumn(name="song_id", referencedColumnName="id")
     *  },
     *  inverseJoinColumns={
     *      @ORM\JoinColumn(name="tag_id", referencedColumnName="id")
     *  })
     * 
     */
    private $tags;

应用\实体\标签

    /**
     * @Groups({"song:read", "song:write"})
     * @ORM\ManyToMany(targetEntity=Song::class, mappedBy="tags")
     */
    private $songs;

我认为这是两者之间的连接表,它没有定义的组。你能帮助我吗? 谢谢

【问题讨论】:

    标签: symfony orm many-to-many entity api-platform.com


    【解决方案1】:

    什么意思:

    当我进入 API 平台 .../api/tags/1 时,我只收到没有“标签”的信息。

    据我了解,您的问题很可能是缺少normalization context 配置的结果。 Tag 端点(例如 /api/tags/1)默认未配置为应用 song:readsong:write 序列化组,导致这些 Tag 字段被排除在响应负载中。

    考虑添加song:readsong:write 作为Tag 端点的默认序列化组。或者最好指定tag:readtag:write 序列化组并将它们添加到Song 序列化组。一个简单的例子:

    <?php declare(strict_types = 1);
    
    namespace App\Entity;
    
    use ApiPlatform\Core\Annotation\ApiResource;
    use Doctrine\Common\Collections\Collection;
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * @ORM\Entity
     * @ApiResource(
     *      itemOperations={
     *          "get"={
     *              "normalization_context"={
     *                  "groups"={
     *                      "song:read",
     *                  },
     *              },
     *          },
     *      },
     * )
     */
    class Song
    {
        /**
         * @Groups({
         *     "song:read",
         *     "tag:read",
         * })
         */
        private Collection $tags;
    }
    
    /**
     * @ORM\Entity
     * @ApiResource(
     *      itemOperations={
     *          "get"={
     *              "normalization_context"={
     *                  "groups"={
     *                      "tag:read",
     *                  },
     *              },
     *          },
     *      },
     * )
     */
    class Tag
    {
        /**
         * @Groups({
         *     "tag:read",
         *     "song:read",
         * })
         */
        private Collection $songs;
    }
    

    PS:考虑到上面的例子建立了一个circular reference

    【讨论】:

    • by > 当我进入 API 平台 .../api/tags/1 时,我只收到没有“标签”的信息。我的意思是,当我提出请求时,我想取回标签。我已经尝试过了,它适用于 oneToMany 和 oneToOne,但不适用于 ManyToMany。我想要``` json { "id": 1, "title": "world", "category": {"id":54, "name":"hello"}, "tags": [{"id" :12, "name":"city"}, {...}] } ``` 而且我没有标签 我认为这确实是两者之间的连接表,它没有定义的组,因为 symfony 有不创建实体,因此没有组...
    【解决方案2】:

    通过

    当我进入 API 平台 .../api/tags/1 时,我只收到没有“标签”的信息。

    我的意思是当我提出请求时,我想取回标签。我已经尝试过了,它适用于 oneToMany 和 oneToOne,但不适用于 ManyToMany。

    我想要

    {
      "id": 1,
      "title": "world",
      "category": {"id":54, "name":"hello"},
      "tags": [{"id":12, "name":"city"}, {...}]
    }
    

    我只有

    {
      "id": 1,
      "title": "world",
      "category": {"id":54, "name":"hello"},
    }
    

    我认为它实际上是两者之间的连接表,它没有定义组,因为 symfony 没有创建实体,因此没有组......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      相关资源
      最近更新 更多