api平台ManyToMany坚持

时间:2020-10-06 04:19:53

标签: symfony api-platform.com

我需要与api平台保持ManyToMany关系,但是我遇到一个错误,我已经尝试使用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;

1 个答案:

答案 0 :(得分:0)

确定要更新架构吗?我测试了您的代码,它可以正常工作