symfony - api平台ManyToMany坚持
问题描述
我需要与 api 平台保持多对多关系,但我得到一个错误,我已经尝试使用 cascade={"persist"} 和组。
当我发送带有对象的请求时,我得到了;属性“idVehicles”的嵌套文档是不允许的。请改用 IRI。
{
"name": "carl",
"lastName": "abs",
"idVehicles": [
{
"license": "1212121"
}
]
}
这是我的用户实体:
/**
* Users
*
* @ORM\Table(name="users")
* @ORM\Entity
* @ApiResource(
* attributes={"pagination_enabled"=false},
* normalizationContext={"groups"={"users_read"}},
* denormalizationContext={"groups"={"users_write"}},
* )
*/
class Users
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="SEQUENCE")
* @ORM\SequenceGenerator(sequenceName="users_id_seq", allocationSize=1, initialValue=1)
* @Groups({"users_read", "users_write"})
*/
private $id;
/**
* @var string|null
*
* @ORM\Column(name="name", type="string", length=128, nullable=true)
* @Groups({"users_read", "users_write"})
*/
private $name;
/**
* @var string|null
*
* @ORM\Column(name="last_name", type="string", length=128, nullable=true)
* @Groups({"users_read", "users_write"})
*/
private $lastName;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Vehicles", mappedBy="idUsers", cascade={"persist"})
* @Groups({"users_read", "users_write"})
*/
private $idVehicles;
}
这是我的车辆实体:
/**
* Users
*
* @ORM\Table(name="users")
* @ORM\Entity
* @ApiResource(attributes={"pagination_enabled"=false})
*/
class Users
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="SEQUENCE")
* @ORM\SequenceGenerator(sequenceName="vehicles_id_seq", allocationSize=1, initialValue=1)
*/
private $id;
/**
* @var string|null
*
* @ORM\Column(name="license", type="string", length=16, nullable=true, cascade={"persist"})
* @Groups({"users_read", "users_write"})
*/
private $license;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Users", inversedBy="idUsers")
* @ORM\JoinTable(name="users_vehicles",
* joinColumns={
* @ORM\JoinColumn(name="id_vehicles", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="id_users", referencedColumnName="id")
* }
* )
*/
private $idUsers;
解决方案
你确定你更新你的架构吗?我测试了你的代码,它正在工作
推荐阅读
- python - 使用 matplotlib 生成等高线图
- java - 为什么 CPU 花费这么多时间执行 getNextJarEntry 方法?
- java - 通过从处理程序接收消息来更新 UI
- ios - GMSAutocompleteViewController 无法正常工作
- java - Spring Boot logback.xml 创建 .tmp 文件
- java - POJO 中的默认请求参数值
- powershell - 我正在尝试在 PowerShell 中以其他用户身份打开一个进程
- java - 如何从某些包中排除 INFO 级别的日志记录
- python-3.x - django 中的 is_active 布尔字段
- unity3d - 继承自 DefaultTrackableEventHandler