Title: Symfony - How to remove a specific element from my array from an entity
Tags: php;symfony;doctrine;entity
Question: 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).

I want to be able to delete one of the elements that are part of the TypeUser array of one of my packages.

My plan of action was:


Retrieve the package to process
Retrieve the name of the TypeUser to remove from the TypeUser array of the package.
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.


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.

Being a beginner in Symfony 3.4, I have a hard time understanding how to do it right.

My controller.php function :

```    /**
     * 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');

```

My entity (paquet) function :

```
    /**  
     * @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);
    }

```

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.

Thanks for your help !

Comment: Yes, that's what I wish if there is only one type left. It works. But if there are several, I want it to just remove the typeUser passed as a parameter.

Comment: Yeah, it's good. Now, I've a new error :

Catchable Fatal Error: Argument 1 passed to Site\PagesBundle\Entity\Paquet181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16leteTypeFromTypesUser() must be an instance of Site\PagesBundle\Entity\TypeUser, array given, called in /usr/local/apache/preprod_8080/htdocs/alatech/src/Site/PagesBundle/Controller/PaquetController.php on line 146 and defined


I have to write the type of my variable at the line : $unPaquet->deleteTypeFromTypesUser($leType);

Comment: Your code is deleting the package itself if theres 1 type.

Comment: Then change this: $em->$unPaquet->deleteTypeFromTypesUser($leType); to $unPaquet->deleteTypeFromTypesUser($leType);

Comment: Check my answer.

Here is the accepted answer: You have to change it on the class, you are calling the $unPaquet property of the entity manager, and then calling to a function that does not exist. Also you forgot to check if there is more than one or you would be deleting from an empty array.

```    /**
     * 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 if ( $nbTypes &gt; 1 )
        {
            $leType = $em-&gt;getRepository('PagesBundle:TypeUser') 
-&gt;findByTypeUtilisateur($type);
            $unPaquet-&gt;deleteTypeFromTypesUser($leType);
        }

        $em-&gt;flush();

        return $this-&gt;redirectToRoute('paquets_index');
    }
```

The "Catchable Fatal Error: Argument 1 passed to Site\PagesBundle\Entity\Paquet181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16leteTypeFromTypesUser() must be an instance of Site\PagesBundle\Entity\TypeUser, array given " error is due to the find returning an array. You have to change that method. If you need help over there show the code please.


Here is another answer: I've updated my 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 if($nbTypes &gt; 1)
    {
        $am = $this-&gt;getDoctrine()-&gt;getManager();
        $leType = $am-&gt;getRepository('PagesBundle:TypeUser')- 
     &gt;findByTypeUtilisateur($type);
        $unPaquet-&gt;deleteTypeFromTypesUser($leType[0]);
    }

    $em-&gt;flush();
```

It's okay now ! Thanks

Comment for this answer: You welcome. Accept my answer if you found it helpful please.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:03/25/19', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<php><symfony><doctrine><entity>', 'Title': 'Symfony - How to remove a specific element from my array from an entity', 'ViewCount': 1917, 'id': 55340334, 'user_AboutMe': None, 'user_AccountId': 15569500.0, 'user_DisplayName': 'Agar io FR', 'user_Reputation': 53.0, 'user_Views': 46.0, 'user_id': 11232324.0}}