title: Symfony - How to remove a specific element from my array from an entity
body: <p>I have a Packages entity (manyToMany with the entityTypeUser that creates me in the DB the package_des_users entity). My Packages entity therefore contains an array of TypeUser (because a package can be assigned to several types of users).</p>
  
  <p>I want to be able to delete one of the elements that are part of the TypeUser array of one of my packages.</p>
  
  <p>My plan of action was:</p>
  
  <ol>
  <li>Retrieve the package to process</li>
  <li>Retrieve the name of the TypeUser to remove from the TypeUser array of the package.</li>
  <li>Do a check: If the package contains only one typeUser, the package is deleted directly. If it contains more than one, just delete the chosen TypeUser.</li>
  </ol>
  
  <p>For now, I was able to recover the package to be processed as well as the name of the TypeUser to delete, and the number of TypeUser that contained my package. If it contains only one, it suppresses it well. The problem remains for the other case.</p>
  
  <p>Being a beginner in Symfony 3.4, I have a hard time understanding how to do it right.</p>
  
  <p>My controller.php function :</p>
  
  <pre><code>    /**
       * Deletes a paquet entity.
       *
       * @Route("/{id}/{type}/delete", name="paquets_delete")
       */
      public function deleteAction(Request $request, $id, $type)
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $unPaquet = $em-&gt;getRepository('PagesBundle:Paquet')-&gt;find($id);
  
          $nbTypes = count($unPaquet-&gt;getTypeUser());
  
          if($nbTypes == 1)
          {
              $em-&gt;remove($unPaquet);
          }
  
          else
          {
              $am = $this-&gt;getDoctrine()-&gt;getManager();
              $leType = $am-&gt;getRepository('PagesBundle:TypeUser') 
  -&gt;findByTypeUtilisateur($type);
              $em-&gt;$unPaquet-&gt;deleteTypeFromTypesUser($leType);
          }
  
          $em-&gt;flush();
  
          return $this-&gt;redirectToRoute('paquets_index');
  
  </code></pre>
  
  <p>My entity (paquet) function :</p>
  
  <pre><code>
      /**  
       * @var \Doctrine\Common\Collections\Collection
       * @ORM\ManyToMany(targetEntity="TypeUser")  
       * @ORM\JoinTable(name="Packages_des_TypesUser") 
       * @ORM\JoinColumn(nullable=false)
       */  
      private $typeUser;
  
      public function __construct()
      {
          $this-&gt;typeUser = new ArrayCollection();
      }
  
      /** 
       * Get TypeUser 
       * 
       * @return Site\PagesBundle\Entity\TypeUser 
       */ 
      public function getTypeUser() 
      { 
          return $this-&gt;typeUser; 
      }
  
      public function deleteTypeFromTypesUser(TypeUser $type)
      {
          $this-&gt;typeUser-&gt;removeElement($type);
      }
  
  </code></pre>
  
  <p>I would therefore like to be able to retrieve the corresponding TypeUser object (the result being unique) from the controller so that I can pass it as a parameter to a function that will take care of removing it from the TypeUser array of my package. Then that translates into the database.</p>
  
  <p>Thanks for your help !</p>