Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3925

How to update OneToMany ressource

$
0
0

I am a beginner in API platform. I am working with two entities Author and Book with a OneToMany relationship:

  • a user can have several books
  • a book can belong to only one Author.

I've tried POST, GET and DELETE methods, they work. However, the PUT method does not work and returns this error:

"hydra:title": "An error occurred",

"hydra:description": **"An exception occurred while executing 'UPDATE Book SET author_id = ? WHERE id = ?' with params [null, 1]:\n\nSQLSTATE[23000]:

Integrity constraint violation: 1048 Column 'author_id' cannot be null"

Here's my code:

/**
 * @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;
/**
 * @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;

Viewing all articles
Browse latest Browse all 3925

Trending Articles