0

这是问题所在:

两个实体 Books 和 Auteurs。一本书只有一个作者,一个作者可以有多本书。

如果我在 Auteurs 表中创建具有现有作者的书,则填充基础时,此书会(错误地)在表中创建副本。

cascade = "persist" 注释的存在是该副本的起源。如果我删除此注释,则会收到错误消息:

{"error": {"code": 500, "message": "Internal Server Error", "exception": [{"message": "通过关系 'App\Entity\Books#auteur' 发现了一个新实体......

如何处理这个问题?我的代码:控制器:

<?php

namespace App\Controller;

use App\Entity\Books;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

class EcritureController extends FOSRestController
{
    /**
     * @Rest\Post(
     *  path = "/creer-book")
     * @Rest\View(StatusCode = 201)
     * @ParamConverter("book", converter="fos_rest.request_body")
     */
    public function creerBook(Books $book)
    {
        $em->persist($book);
        $em->flush();
        return $book;
    }
}

实体书:

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\BooksRepository")
 */
class Books
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $titre;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Books", inversedBy="Books", cascade={"persist"})
     * @ORM\JoinColumn(nullable=false)
     */
    private $auteur;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTitre(): ?string
    {
        return $this->Titre;
    }

    public function setTitre(string $titre): self
    {
        $this->titre = $titre;

        return $this;
    }

    public function getAuteur(): ?Auteurs
    {
        return $this->auteur;
    }

    public function setAuteur(?Auteurs $auteur): self
    {
        $this->auteur = $auteur;

        return $this;
    }
}

实体作者:

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\AuteursRepository")
 */
class Auteurs
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=10)
     */
    private $nom;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Books", mappedBy="auteur") 
     */
    private $books;

    public function __construct()
    {
        $this->books = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getNom(): ?string
    {
        return $this->nom;
    }

    public function setNom(string $nom): self
    {
        $this->nom = $nom;

        return $this;
    }

    /**
     * @return Collection|Books[]
     */
    public function getBooks(): Collection
    {
        return $this->books;
    }

    public function addBook(Books $book): self
    {
        if (!$this->books->contains($book)) {
            $this->books[] = $book;
            $book->setBook($this);
        }
        return $this;
    }

    public function removeBook(Books $book): self
    {
        if ($this->books->contains($book)) {
            $this->sujets->removeElement($book);
            // set the owning side to null (unless already changed)
            if ($book->getAuteur() === $this) {
                $book->setAuteur(null);
            }
        }
        return $this;
    }
}
4

1 回答 1

0

错误是“坚持”“许多”方面。必须坚持“一”方,如下:

        $auteurId = $book->getAuteur()->getId();
        $auteur= $em->getRepository('App:Auteurs')->find($auteurId);
        $response = new Response();
        if ($auteur) {
            $auteur->addBook($book);
            $em->persist($auteur);
            $em->flush();
            $response->setContent('Chain of your choice', Response::HTTP_OK);
        } else {
            $response->setContent('Auteur selected doesn\'t exist', Response::HTTP_NOT_ACCEPTABLE);
        }

        return $response;
于 2020-01-23T07:36:19.350 回答