Witam,
mam problem podczas tworzenia relacji w encjach, gdy chcę skorzystać z doctrine bidirectional to wyskakuje mi błąd:
A circular reference has been detected when serializing the object of class "App\Entity\Project" (configured limit: 1)
Poniżej podaje moje encje i kontroler:
App\Entity\Client:
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\ClientRepository")
*/
class Client {
[...]
/**
* @ORM\ManyToMany(targetEntity=Project::class, mappedBy="clients")
* @Groups({"clients"})
*/
private $projects;
public function __construct() {
$this->dateCreate = new \DateTime();
$this->projects = new ArrayCollection();
}
[...]
/**
* @return Collection|Project[]
*/
public function getProjects(): Collection
{
return $this->projects;
}
public function addProject(Project $project): self
{
if (!$this->projects->contains($project)) {
$this->projects[] = $project;
$project->addClient($this);
}
return $this;
}
public function removeProject(Project $project): self
{
if ($this->projects->removeElement($project)) {
$project->removeClient($this);
}
return $this;
}
}
App\Entity\Project:
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\ProjectRepository")
*/
class Project {
[..]
/**
* @ORM\ManyToMany(targetEntity=Client::class, inversedBy="projects")
*/
private $clients;
public function __construct() {
$this->dateCreate = new \DateTime();
$this->clients = new ArrayCollection();
}
[...]
/**
* @return Collection|Client[]
*/
public function getClients(): Collection
{
return $this->clients;
}
public function addClient(Client $client): self
{
if (!$this->clients->contains($client)) {
$this->clients[] = $client;
}
return $this;
}
public function removeClient(Client $client): self
{
$this->clients->removeElement($client);
return $this;
}
}
App\Controller\ProjectController:
/**
* @Route("/project/{id}",
* name="projectsSingle",
* methods={"GET"},
* requirements={"id" = "\d+"})
*/
public function getSingle($id) {
$Project = $this->getDoctrine()
->getRepository(Project::class)
->findOneBy(array('id' => $id, 'active' => 1));
if (null == $Project) {
throw $this->createNotFoundException();
}
$data = array(
'id' => $Project->getId(),
'title' => $Project->getTitle(),
'type' => $Project->getType(),
'status' => $Project->getStatus(),
'description' => $Project->getDescription(),
'dateStart' => $Project->getDateStart(),
'dateEnd' => $Project->getDateEnd(),
'contractNumber' => $Project->getContractNumber(),
'value' => $Project->getValue(),
'subsidy' => $Project->getSubsidy(),
'contribution' => $Project->getContribution(),
'dateCreate' => $Project->getDateCreate(),
'clients' => $Project->getClients(),
);
return $this->json([
'code' => 200,
'content' => $data,
]);
}
Ma ktoś jakiś pomysł żeby rozwiązać ten problem?