Symfony 如何更新OneToMany ressource

Symfony 如何更新OneToMany ressource,symfony,symfony4,api-platform.com,Symfony,Symfony4,Api Platform.com,我是API平台的初学者。我与两个实体Author和Book有OneToMany关系: 一个用户可以有几本书 一本书只能属于一位作者 我尝试了POST,GET和DELETE方法,它们都很有效。但是,PUT方法不起作用并返回此错误: “hydra:title”:“发生错误” “hydra:description”:**“使用参数[null,1]:\n\nSQLSTATE[23000]执行“更新图书集作者\u id=?其中id=?”时发生异常: 完整性约束冲突:1048列“作者id”不能为空 这是

我是API平台的初学者。我与两个实体
Author
Book
OneToMany
关系:

  • 一个用户可以有几本书
  • 一本书只能属于一位作者
我尝试了
POST
GET
DELETE
方法,它们都很有效。但是,
PUT
方法不起作用并返回此错误:

“hydra:title”:“发生错误”

“hydra:description”:**“使用参数[null,1]:\n\nSQLSTATE[23000]执行“更新图书集作者\u id=?其中id=?”时发生异常:

完整性约束冲突:1048列“作者id”不能为空

这是我的密码:

/**
 * @ORM\Entity(repositoryClass="App\Repository\AuthorRepository")
 * @ApiResource(
 *          normalizationContext={"groups"={"book:output"}},
 *          denormalizationContext={"groups"={"book:input"}}
 * )
 */
class Author
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"book:input"})
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"book:output", "book:input"})
     */
    private $firstname;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"book:output", "book:input"})
     */
    private $lastname;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Book", mappedBy="author", cascade={"all"})
     * @Groups({"book:output", "book:input"})
     */
    private $books;

您似乎要为更新的实体传递一个空id,这是ORM必须知道的。

错误消息的确切内容:
完整性约束冲突:1048列“author\u id”不能为空“
。在PUT过程中,您不能删除该作者,只能切换到另一个。
/**
 * @ORM\Entity(repositoryClass="App\Repository\BookRepository")
 * @ApiResource()
 */
class Book
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"book:output", "book:input"})
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"book:output", "book:input"})
     */
    private $name;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Author", inversedBy="books")
     * @ORM\JoinColumn(nullable=false)
     */
    private $author;