【问题标题】:POST request on many-to-many association多对多关联的 POST 请求
【发布时间】:2016-03-09 18:51:47
【问题描述】:

我有两个具有多对多关联的实体:

class User extends BaseUser

class Calendar
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

    /**
     * @var string
     *
     * @ORM\Column(name="view", type="text")
     * @Assert\Choice(choices = {"work week", "week", "month", "year"}, message = "Choose a valid view.")
     */
    private $view;

    /**
     * @ManyToMany(targetEntity="AppBundle\Entity\User")
     * @JoinTable(name="calendars_publishers",
     *      joinColumns={@JoinColumn(name="calendar_id", referencedColumnName="id")},
     *      inverseJoinColumns={@JoinColumn(name="publisher_id", referencedColumnName="id", unique=true)}
     *      )
     */
    private $publishers;

    public function __construct()
    {
        $this->publishers = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
      * Add publishers
      *
      * @param \AppBundle\Entity\User $publishers
      * @return Calendar
      */
      public function addPublisher(\AppBundle\Entity\User $publishers)
       {
          $this->publishers[] = $publishers;

           return $this;
       }

      /**
        * Remove publishers
        *
        * @param \AppBundle\Entity\User $publishers
        */
       public function removePublisher(\AppBundle\Entity\User $publishers)
     {
         $this->publishers->removeElement($publishers);
      }
}

当我使用正文对我的 REST API (http://localhost:8000/calendars) 发出 POST 请求时:

{
  "name": "My calendar",
  "view": "week",
  "publishers": [
    "/users/1",
    "/users/2"
  ]
}

我有回应:

{
  "@context": "/contexts/Calendar",
  "@id": "/calendars/3",
  "@type": "Calendar",
  "name": "My calendar",
  "view": "week",
  "publishers": []
}

所以,如您所见,我的对象记录良好,但我无法将一些用户放入publishers。你有什么想法吗?

我使用捆绑包:

  • DunglasApiBundle
  • NelmioApiDocBundle
  • NelmioCorsBundle
  • FOSHttpCacheBundle
  • FOSUserBundle
  • LexikJWTAuthenticationBundle

api-platform

【问题讨论】:

    标签: symfony fosrestbundle api-platform.com


    【解决方案1】:

    您应该添加addPublisher(User $publisher)removePublisher(User $publisher) 方法。

    API 平台内部使​​用 Symfony PropertyAccess 组件,该组件需要此类方法才能访问私有属性。

    【讨论】:

    • 我已经有了这些方法,就像我在我的帖子中编辑添加它一样。
    • 你可以尝试添加一个 setPublishers 方法(不删除加法器和删除器)吗?
    【解决方案2】:

    听起来您应该在关系中指定fetch="EAGER",以便它始终获取相关对象。

    http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#manytoone (还有下一段)

    这里有一些很好的信息:http://www.uvd.co.uk/blog/some-doctrine-2-best-practices/

    【讨论】:

    • 指定fetch="EAGER"什么都不做,我不知道为什么。所以我使用Serialization groups and relations 并且它有效。我不知道这是否是最干净的方法。
    猜你喜欢
    • 2017-09-09
    • 2018-09-07
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    相关资源
    最近更新 更多