Doctrine 2 avec Symfony 4.2.5 et FOSRestBundle - OneToMany a



这是问题所在:

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

当我在 Auteurs 表中使用现有作者创建一本书时,填充基数时,这本书写会在表中创建(错误地(重复项。

级联 = "持久"注释的存在是此副本的原点。如果我删除此注释,则会出现错误:

{"错误": {"代码": 500, "消息": ">

内部服务器错误", "异常": [{"消息": "通过关系 'App \ Entity \ Books # auteur' 找到了一个新实体.....

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

<?php
namespace AppController;
use AppEntityBooks;
use FOSRestBundleControllerAnnotations as Rest;
use FOSRestBundleControllerFOSRestController;
use SensioBundleFrameworkExtraBundleConfigurationParamConverter;
class EcritureController extends FOSRestController
{
/**
* @RestPost(
*  path = "/creer-book")
* @RestView(StatusCode = 201)
* @ParamConverter("book", converter="fos_rest.request_body")
*/
public function creerBook(Books $book)
{
$em->persist($book);
$em->flush();
return $book;
}
}

实体书籍:

<?php
namespace AppEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity(repositoryClass="AppRepositoryBooksRepository")
*/
class Books
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMColumn(type="string", length=255)
*/
private $titre;
/**
* @ORMManyToOne(targetEntity="AppEntityBooks", inversedBy="Books", cascade={"persist"})
* @ORMJoinColumn(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 AppEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity(repositoryClass="AppRepositoryAuteursRepository")
*/
class Auteurs
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMColumn(type="string", length=10)
*/
private $nom;
/**
* @ORMOneToMany(targetEntity="AppEntityBooks", 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;
}
}

错误是"坚持"多"端。有必要坚持"一"端,如下所示:

$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;

相关内容

  • 没有找到相关文章

最新更新