I'm getting a circular reference error using ManyToOne on an entity object. My entities look like:
class CronjobProperties
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Cronjobs", inversedBy="cronjobProperties")
* @ORM\JoinColumn(onDelete="CASCADE", nullable=false)
*/
private $cronjob;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\CronjobProperties", inversedBy="children")
* @ORM\JoinColumn(onDelete="CASCADE")
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity="App\Entity\CronjobProperties", mappedBy="parent", orphanRemoval=true, cascade={"persist"})
*/
private $children;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $value;
}
...
class CoreConfigData
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Cronjobs")
* @ORM\JoinColumn(onDelete="CASCADE", nullable=false)
*/
private $scope;
/**
* @ORM\Column(type="string", length=255)
*/
private $scopeName;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $value;
/**
* @ORM\Column(type="string", length=255)
*/
private $path;
/**
* @ORM\Column(type="string", length=255)
*/
private $environment;
...
class Cronjobs
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=255)
*/
private $command;
/**
* @ORM\OneToMany(targetEntity="App\Entity\CronjobProperties", mappedBy="cronjob", orphanRemoval=true)
*/
private $cronjobProperties;
/**
* @ORM\OneToMany(targetEntity="App\Entity\CoreConfigData", mappedBy="scope", orphanRemoval=true)
*/
private $coreSettings;
/**
* @return Collection|CronjobProperties[]
*
*/
public function getCronjobProperties(): Collection
{
return $this->cronjobProperties;
}
Basically the 'Cronjobs' table is related with OneToMany to the 'CronjobProperties' table using the 'cronjob' field on the 'CronjobProperties' table and with OneToMany to the 'CoreConfigData' table using the 'scope' field. So both CronjobProperties and CoreConfigData are connected to Cronjobs table.
I'm getting the issue when I introduced the third entity and added another OneToMany to the Cronjobs table
I also tried with Groups and MaxDepth changing the Cronjobs entity like this:
/**
* @ORM\Entity(repositoryClass="App\Repository\CronjobsRepository")
* @ORM\Table(name="w_cronjobs", uniqueConstraint={@ORM\UniqueConstraint(name="unique_name_command", columns={"name", "command"})})
*/
class Cronjobs
{
/**
* @Groups("cronjob")
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @Groups("cronjob")
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @Groups("cronjob")
* @ORM\Column(type="string", length=255)
*/
private $command;
/**
* @MaxDepth(2)
* @ORM\OneToMany(targetEntity="App\Entity\CronjobProperties", mappedBy="cronjob", orphanRemoval=true)
*/
private $cronjobProperties;
/**
* @MaxDepth(2)
* @ORM\OneToMany(targetEntity="App\Entity\CoreConfigData", mappedBy="scope", orphanRemoval=true)
*/
private $coreSettings;
/**
* @return Collection|CronjobProperties[]
*
*/
public function getCronjobProperties(): Collection
{
return $this->cronjobProperties;
}