title: A circular reference has been detected when serializing the object of class "App\Entity\User" (configured limit: 1)
body: <p>I am faced with a problem that gives me this error:</p>
  <blockquote>
  <p>A circular reference has been detected when serializing the object of
  class &quot;App\Entity\User&quot; (configured limit: 1)</p>
  </blockquote>
  <p>I have an Enterprise entity that has mission orders, vehicles, and users.</p>
  <p>An orders entity that has a relationship with a User, Company, and Vehicle.</p>
  <p>And a User entity that has a relationship with orders and company.</p>
  <p>So I have this:
  <strong>Entreprise.php</strong></p>
  <pre><code>class Entreprise
  {
      /**
       * @ORM\Id()
       * @ORM\GeneratedValue()
       * @ORM\Column(type=&quot;integer&quot;)
       */
      private $id;
  
  
      /**
       * @ORM\OneToMany(targetEntity=&quot;App\Entity\User&quot;, mappedBy=&quot;entreprise&quot;, orphanRemoval=true)
       */
      private $users;
  
      /**
       * @ORM\OneToMany(targetEntity=&quot;App\Entity\Vehicule&quot;, mappedBy=&quot;entreprise&quot;, orphanRemoval=true)
       */
      private $vehicules;
  
      /**
       * @ORM\OneToMany(targetEntity=&quot;App\Entity\OrdreMission&quot;, mappedBy=&quot;entreprise&quot;, orphanRemoval=true)
       */
      private $ordreMissions;
  </code></pre>
  <p><strong>OrdreMission.php:</strong></p>
  <pre><code>class OrdreMission
  {
  
      /**
       * @ORM\Id()
       * @ORM\GeneratedValue()
       * @ORM\Column(type=&quot;integer&quot;)
       */
      private $id;
  
      /**
       * Agent qui réalisera la mission
       * @ORM\ManyToOne(targetEntity=&quot;App\Entity\User&quot;, inversedBy=&quot;ordreMissions&quot;)
       * @ORM\JoinColumn(nullable=false)
       */
      private $user;
  
  
      /**
       * Immatriculation de la voiture de service
       * @ORM\ManyToOne(targetEntity=&quot;App\Entity\Vehicule&quot;)
       */
      private $vehicule;
  
  
  
      /**
       * @ORM\ManyToOne(targetEntity=&quot;App\Entity\Entreprise&quot;, inversedBy=&quot;ordreMissions&quot;)
       * @ORM\JoinColumn(nullable=false)
       */
      private $entreprise;
  </code></pre>
  <p><strong>Vehicule.php:</strong></p>
  <pre><code>class Vehicule
  {
      /**
       * @ORM\Id()
       * @ORM\GeneratedValue()
       * @ORM\Column(type=&quot;integer&quot;)
       */
      private $id;
  
      /**
       * Marque du véhicule
       * @ORM\Column(type=&quot;string&quot;, length=255)
       */
      private $marque;
  
      /**
       * Modèle du véhicule
       * @ORM\Column(type=&quot;string&quot;, length=255)
       */
      private $modele;
  
      /**
       * Immatriculation du véhicule
       * @ORM\Column(type=&quot;string&quot;, length=255)
       * @MaxDepth(2)
       */
      private $immatriculation;
  
      /**
       * @ORM\ManyToOne(targetEntity=&quot;App\Entity\Entreprise&quot;, inversedBy=&quot;vehicules&quot;)
       * @ORM\JoinColumn(nullable=false)
       * @MaxDepth(2)
       */
      private $entreprise;
  </code></pre>
  <p><strong>User.php:</strong></p>
  <pre><code>class User implements UserInterface, Serializable
  {
      /**
       * @ORM\Id()
       * @ORM\GeneratedValue()
       * @ORM\Column(type=&quot;integer&quot;)
       */
      private $id;
  
      /**
       * Adresse email de l'utilisateur
       * @ORM\Column(type=&quot;string&quot;, length=180, unique=true)
       * @Assert\NotBlank()
       * @Assert\Email(message=&quot;Veuillez renseigner un email valide&quot;)
       */
      private $email;
  
      /**
       * Rôles de l'utilisateur
       * @ORM\Column(type=&quot;json&quot;)
       */
      private $roles = [];
  
  
      /**
       * Ordres de mission de l'utilisateur
       * @ORM\OneToMany(targetEntity=&quot;App\Entity\OrdreMission&quot;, mappedBy=&quot;user&quot;)
       */
      private $ordreMissions;
  
  
      /**
       * @ORM\ManyToOne(targetEntity=&quot;App\Entity\Entreprise&quot;, inversedBy=&quot;users&quot;)
       * @ORM\JoinColumn(nullable=false)
       */
      private $entreprise;
  
  /**
   * String representation of object
   * @link http://php.net/manual/en/serializable.serialize.php
   * @return string the string representation of the object or null
   */
  public function serialize()
  {
      return serialize([
          $this-&gt;id,
          $this-&gt;email,
          $this-&gt;password,
      ]);
  }
  
  /**
   * Constructs the object
   * @link http://php.net/manual/en/serializable.unserialize.php
   * @param string $serialized &lt;p&gt;
   * The string representation of the object.
   * &lt;/p&gt;
   * @return void
   */
  public function unserialize($serialized)
  {
      list (
          $this-&gt;id,
          $this-&gt;email,
          $this-&gt;password,
          ) = unserialize($serialized);
  }
  </code></pre>
  <p>When I want to add a new vehicle, I get the error:</p>
  <blockquote>
  <p>A circular reference has been detected when serializing the object of
  class &quot;App\Entity\User&quot; (configured limit: 1)</p>
  </blockquote>
  <p>I saw on the Internet that I had to do something with a &quot;maxdepth&quot;, but I do not understand what I have to do and where exactly</p>
  <p>This is the function controller that I use to add a Vehicle Object and send it:</p>
  <pre><code>   /**
       * Pour créer un nouveau véhicule
       * 
       * @Route(&quot;/chef-service/ordres-mission/new/new-vehicule&quot;, name=&quot;vehicule_create&quot;)
       * @IsGranted({&quot;ROLE_CHEF_SERVICE&quot;})
       * @Method({&quot;POST&quot;})
       * @return Response
       */
      public function createVehicule(Request $request, EntityManagerInterface $manager)
      {
          $vehicule = new Vehicule();
          $vehicule-&gt;setEntreprise($this-&gt;adminService-&gt;getEntreprise());
  
          $form = $this-&gt;createForm(VehiculeType::class, $vehicule, [
              'action' =&gt; $this-&gt;generateUrl($request-&gt;get('_route'))
          ]);
          $form-&gt;handleRequest($request);
  
          if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
  
              $encoders = array(new XmlEncoder(), new JsonEncoder());
              $normalizers = array(new ObjectNormalizer());
              $serializer = new Serializer($normalizers, $encoders);
              $manager-&gt;persist($vehicule);
              $manager-&gt;flush();
  
              $result = $serializer-&gt;normalize(
                  [
                      'code' =&gt; 200,
                      'message' =&gt; 'OK',
                      'vehicule' =&gt; $vehicule,
                  ],
                  null,
                  [AbstractObjectNormalizer::ENABLE_MAX_DEPTH =&gt; true]
              );
              
              $jsonContent = $serializer-&gt;serialize(
                  $result,
                  'json'
              );
              return new Response($jsonContent);
  
          }
  
          return $this-&gt;render('ordre_mission/partials/newVehicule.html.twig', [
              'formVehicule' =&gt; $form-&gt;createView(),
          ]);
      }
  </code></pre>