对多对多关联的POST请求 [英] POST request on many-to-many association

查看:79
本文介绍了对多对多关联的POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  class用户扩展BaseUser 

 班级日历{/*** @var整数** @ORM \ Column(name ="id",type ="integer")* @ORM \ Id* @ORM \ GeneratedValue(strategy ="AUTO")*/私人$ id;/*** @var字符串** @ORM \ Column(name ="name",type ="text")*/私人$ name;/*** @var字符串** @ORM \ Column(name ="view",type ="text")* @Assert \ Choice(选择= {工作周",周",月",年"},消息=选择有效的视图.")*/私人$ 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)}*)*/私人$ publishers;公共功能__construct(){$ this-> publishers = new \ Doctrine \ Common \ Collections \ ArrayCollection();}/***添加发布者** @param \ AppBundle \ Entity \ User $ publishers* @返回日历*/公共功能addPublisher(\ AppBundle \ Entity \ User $ publishers){$ this-> publishers [] = $ publishers;返回$ this;}/***删除发布者** @param \ AppBundle \ Entity \ User $ publishers*/公共函数removePublisher(\ AppBundle \ Entity \ User $ publishers){$ this-> publishers-> removeElement($ publishers);}} 

当我在REST API上执行POST请求时( http://localhost:8000/calendars )与身体:

  {"name":我的日历","view":"week",发布者":["/users/1","/用户/2"]} 

我有回应:

  {"@context":"/contexts/Calendar","@id":"/calendars/3","@type":日历","name":我的日历","view":"week",发布者":[]} 

因此,如您所见,我的对象录制得很好,但是我不能将某些用户放在发布者中.你有主意吗?

我使用捆绑包:

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

具有 api平台.

解决方案

您应该添加 addPublisher(User $ publisher) removePublisher(User $ publisher)方法./p>

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

I have two entities with a many-to-many association:

class User extends BaseUser

and

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);
      }
}

And when I do a POST request on my REST API (http://localhost:8000/calendars) with the body:

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

I have the response:

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

So, as you see, my object is recorded well, but I cannot put some users in publishers. Do you have an idea?

I use the bundles:

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

with api-platform.

解决方案

You should add addPublisher(User $publisher) and removePublisher(User $publisher) methods.

API Platform internally uses the Symfony PropertyAccess component, and this component requires such methods to be able to access to the private property.

这篇关于对多对多关联的POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆