title: Association mapping : manytomany Symfony
body: <p>I currently have 2 Visitor and Visit entities and I made a many to many relationship but where I get stuck is how to know which entity is owner and which entity is not.</p>
  <p>For me the table that manages everything is Visit I want to make a CRUD of its two entities for visitor no problem but for the visit entity there it complicates I manage to display the list of my visits to read them and to them delete but for edit and create sa doesn't work.</p>
  <p>I think first of all <strong>I have an error concerning the inversedby and mapped by of my two entities.</strong></p>
  <p>But also concerning my <strong>edit and new methods of my VisiteController.</strong></p>
  <p>Entity Visite :</p>
  <pre><code>&lt;?php
   
  ****
  /**
   * @ORM\Entity(repositoryClass=VisiteRepository::class)
   */
  class Visite
  {
      /**
       * @ORM\Id()
       * @ORM\GeneratedValue()
       * @ORM\Column(type=&quot;integer&quot;)
       */
      private $id;
   
      /**
       * @ORM\Column(type=&quot;string&quot;, length=255, nullable=true)
       */
      private $libelle;
   
       
      /**
       * @ORM\ManyToMany(targetEntity=Personne::class, mappedBy=&quot;visite&quot;)
       */
      private $personnes;
   
   
      public function __construct()
      {
          $this-&gt;personnes = new ArrayCollection();
      }
   
      public function getId(): ?int
      {
          return $this-&gt;id;
      }
   
      public function getLibelle(): ?string
      {
          return $this-&gt;libelle;
      }
   
      public function setLibelle(?string $libelle): self
      {
          $this-&gt;libelle = $libelle;
   
          return $this;
      }
   
      /**
       * @return Collection|Personne[]
       */
      public function getPersonnes(): Collection
      {
          return $this-&gt;personnes;
      }
   
      public function addPersonne(Personne $personne): self
      {
          if (!$this-&gt;personnes-&gt;contains($personne)) {
              $this-&gt;personnes[] = $personne;
              $personne-&gt;addVisite($this);
          }
   
          return $this;
      }
   
      public function removePersonne(Personne $personne): self
      {
          if ($this-&gt;personnes-&gt;contains($personne)) {
              $this-&gt;personnes-&gt;removeElement($personne);
              $personne-&gt;removeVisite($this);
          }
   
          return $this;
      }
   
   
  }
  </code></pre>
  <p>Entity Personne/Visiteur :</p>
  <pre><code>&lt;?php
   
  namespace App\Entity;
   
  use App\Repository\PersonneRepository;
  use Doctrine\Common\Collections\ArrayCollection;
  use Doctrine\Common\Collections\Collection;
  use Doctrine\ORM\Mapping as ORM;
   
  /**
   * @ORM\Entity(repositoryClass=PersonneRepository::class)
   */
  class Personne
  {
      /**
       * @ORM\Id()
       * @ORM\GeneratedValue()
       * @ORM\Column(type=&quot;integer&quot;)
       */
      private $id;
   
      /**
       * @ORM\Column(type=&quot;string&quot;, length=255, nullable=true)
       */
      private $nom;
   
      /**
       * @ORM\Column(type=&quot;string&quot;, length=255, nullable=true)
       */
      private $email;
   
   
      /**
       * @ORM\ManyToMany(targetEntity=Visite::class, inversedBy=&quot;personnes&quot;)
       */
      private $visite;
   
      public function __construct()
      {
          $this-&gt;visite = new ArrayCollection();
      }
   
      public function getId(): ?int
      {
          return $this-&gt;id;
      }
   
      public function getNom(): ?string
      {
          return $this-&gt;nom;
      }
   
      public function setNom(?string $nom): self
      {
          $this-&gt;nom = $nom;
   
          return $this;
      }
   
   
      public function getEmail(): ?string
      {
          return $this-&gt;email;
      }
   
      public function setEmail(?string $email): self
      {
          $this-&gt;email = $email;
   
          return $this;
      }
   
   
      /**
       * @return Collection|Visite[]
       */
      public function getVisite(): Collection
      {
          return $this-&gt;visite;
      }
   
      public function addVisite(Visite $visite): self
      {
          if (!$this-&gt;visite-&gt;contains($visite)) {
              $this-&gt;visite[] = $visite;
          }
   
          return $this;
      }
   
      public function removeVisite(Visite $visite): self
      {
          if ($this-&gt;visite-&gt;contains($visite)) {
              $this-&gt;visite-&gt;removeElement($visite);
          }
   
          return $this;
      }
   
  }
  </code></pre>
  <p>When creating a new visit, it registers itself in the database but unfortunately its only insert on the visit table, I have nothing to add on the person_visit associative table.</p>
  <p>Visit Controller:</p>
  <pre><code>/**
       * @Route(&quot;/new&quot;, name=&quot;visite_new&quot;, methods={&quot;GET&quot;,&quot;POST&quot;})
       */
      public function new(Request $request): Response
      {
          $visite = new Visite();
          $form = $this-&gt;createForm(VisiteType::class, $visite);
          $form-&gt;handleRequest($request);
   
          if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
              $entityManager = $this-&gt;getDoctrine()-&gt;getManager();
              $entityManager-&gt;persist($visite);
              $entityManager-&gt;flush();
   
              return $this-&gt;redirectToRoute('visite_index');
          }
   
          return $this-&gt;render('visite/new.html.twig', [
              'visite' =&gt; $visite,
              'form' =&gt; $form-&gt;createView(),
          ]);
      }
   
   
      /**
       * @Route(&quot;/{id}/edit&quot;, name=&quot;visite_edit&quot;, methods={&quot;GET&quot;,&quot;POST&quot;})
       * @param Visite $visite
       * @param Request $request
       * @return \Symfony\Component\HttpFoundation\Response
       */
      public function edit(Visite $visite, Request $request)
      {
          $personne = new Personne();
          $visite-&gt;addPersonne($personne);
   
          $form = $this-&gt;createForm(VisiteType::class, $visite);
          $form-&gt;handleRequest($request);
   
          if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
  /*            $entityManager = $this-&gt;getDoctrine()-&gt;getManager();
              $entityManager-&gt;persist($visite, $personne);
              $entityManager-&gt;flush();*/
              $this-&gt;getDoctrine()-&gt;getManager()-&gt;flush();
             /* $this-&gt;addFlash('success', 'Visite ajouté avec succès');*/
   
              return $this-&gt;redirectToRoute('visite_index');
          }
   
          return $this-&gt;render('visite/edit.html.twig', [
              'visite' =&gt; $visite,
              'form' =&gt; $form-&gt;createView(),
          ]);
      }
  </code></pre>
  <p>Thanks for your help</p>