0

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;
    }
Claudio Ferraro
  • 4,551
  • 6
  • 43
  • 78
  • I don't understand how serialization groups can help on that answer. I'm getting the issue when I introduced the third entity and added another OneToMany to the Cronjobs table. In that question the person is getting the issue when creating a new entity. I'm getting the issue while fetching data. – Claudio Ferraro Nov 17 '20 at 13:09
  • 1
    This one as well https://stackoverflow.com/questions/59268438/symfony-4-a-circular-reference-has-been-detected-when-serializing-the-object-o – yivi Nov 17 '20 at 13:10
  • Serialization groups are used when fetching data, exactly. – yivi Nov 17 '20 at 13:10
  • I tried both options proposed there but they don't work. I will update in a moment the question – Claudio Ferraro Nov 17 '20 at 13:38
  • I now discovered and that's odd, that if I remove the getCrojobProperties GETTER , it works. If I Ignore it to the serializer it doesn't work. ?? – Claudio Ferraro Nov 17 '20 at 14:12
  • I guess it’s something with the lazy loading. Try to avoid the lazy loading and fetch the entire data before serializing. – Alexander Dimitrov Nov 18 '20 at 17:16

0 Answers0