我有两个具有多对多关联的实体:
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);
}
}
当我对 REST API 执行 POST 请求时(http://localhost:8000/日历 http://localhost:8000/calendars)与身体:
{
"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
- NelmioCors 捆绑包
- FOSHttpCacheBundle
- FOS用户包
- LexikJWTAuthenticationBundle
with API平台 https://api-platform.com/.