POST request on many-to-many association

余生长醉 提交于 2021-02-16 15:27:10

问题


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.


回答1:


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.




回答2:


It sounds like you should specify fetch="EAGER" in the relationship so that it always gets the related objects.

see http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#manytoone (and also the next paragraph)

and some good info here: http://www.uvd.co.uk/blog/some-doctrine-2-best-practices/



来源:https://stackoverflow.com/questions/35900158/post-request-on-many-to-many-association

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!